如何将两个或多个值/变量连接到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']),
]);
}
}
我对laravel(5.2)几乎是新手,这里我想连接两个值,比如名字和姓氏 在用户注册期间将其保存到数据库中,老实说,作为这个整体概念的新手,我很少想到这样做......如果我能得到一些帮助那就太棒了.... p>
User.php strong> p>
AuthController.php strong> p>
class用户扩展Authenticatable
{
/ **
*可批量分配的属性。\ n *
* @var数组
* /
protected $ fillable = [
'first_name','last_name','email'
];
/ **
*应该属于的属性 为数组隐藏。
*
* @var array
* /
protected $ hidden = [
'密码','remember_token',
];
}
code> pre>
类AuthController扩展Controller
{
protected function validator(array $ data)
{
返回Validator :: make($ data,[
'first_name'=> 'required | max:255',
'last_name'=> 'required | max:255',
'email'=> 'required | email | max:255 | unique:users',
'password'=> 'required | min:6 |确认',
]);
}
保护函数create(array $ data)
{
return User :: create([
'irst_name'=> $ data ['first_name'],
'last_name'=> $ data ['last_name'],
'email'=> $ data ['email'],
'password'=> bcrypt($ data ['password']),
]);
}
}
code> pre>
div>
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
Concatenation of two strings in a php concept, it do not relates to laravel in any way. You can easily concatenate two or more string like:
$str = $data['first_name'].' '.$data['last_name']; // concatenate by using space
But this does not help you because you are inserting user data into user table which have first_name, last_name columns. So you have to insert them individually.