doushi2902 2016-12-01 11:44
浏览 29
已采纳

如何将两个或多个值/变量连接到laravel中的新变量

I am pretty much new to laravel(5.2), here i want to concatenate two values say first name and last name during user registration and save it to database, honestly as am new to this whole concept i have very little idea of doing it...it would be great if i could get some help....

User.php

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name', 'email'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

AuthController.php

class AuthController extends Controller
{
  protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
             ]);
    }

protected function create(array $data)
    {
        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
             ]);
    }
}
  • 写回答

2条回答 默认 最新

  • dongliao3450 2016-12-01 12:30
    关注

    if you want to save the new value to data base you must register it in you fillable, also to alter the database to accept the new column

    protected $fillable = [
        'user_name', 'first_name', 'last_name', 'email'
    ];
    
    
    protected function create(array $data)
    {
        return User::create([
            'user_name' => $data['first_name'].' '.$data['last_name'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
             ]);
    }
    

    you may want to remove first_name and last_name.. and store only full name, or you can ignore storing the full name, and in User.php model you can override the toArray() method and make it returns the full name which you can concatenate just like above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了