我试图通过Laravel中的表单向数据库中插入一个值,但是我的所有值都没有插入。
仅插入电子邮件,密码,创建日期
这是我的迁移代码:-
Schema::defaultStringLength(191);
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fname');
$table->string('lname');
$table->string('email')->unique();
$table->string('phone');
$table->string('gender');
$table->string('dob');
$table->string('religion');
$table->string('mtn');
$table->string('country');
$table->string('city');
$table->string('district');
$table->string('upozila');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
这是我的控制器代码:-
public function register(Request $request)
{
$this->validation($request);
User::create($request->all());
return redirect('/');
}
public function validation($request)
{
$validatedData = $request->validate([
'fname' => 'required|max:255',
'lname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users,email',
'phone' => 'required|max:255',
'gender' => 'required|max:255',
'dob' => 'required|max:255',
'religion' => 'required|max:255',
'mtn' => 'required|max:255',
'country' => 'required|max:255',
'city' => 'required|max:255',
'district' => 'required|max:255',
'upozila' => 'required|max:255',
'password' => 'required|min:6',
'confirm_password' =>'required|min:6|same:password',
]);
}
这是我的数组= $ request-> all();
_token "wDRoDeLkOFX5re5nba2Ufv5pr0iKzYVCr0tK9EFE"
fname "nirab"
lname "nirax"
email "is@gmail.com"
phone "988907"
gender "male"
dob "2018-12-03"
religion "male"
mtn "male"
country "male"
city "male"
district "male"
upozila "male"
password "1234567"
confirm_password "1234567"
答案 0 :(得分:1)
您可以使用create
方法将新模型保存在一行中。插入的模型实例将从该方法返回给您。但是,在执行此操作之前,您需要在模型上指定fillable
或guarded
属性,因为默认情况下,所有Eloquent模型都可以防止大规模分配。
您可以指定在模型中可以批量分配的字段,可以通过在模型中添加特殊变量$fillable
来完成。因此在模型中:
class users extends Model {
protected $fillable = ['fname', 'lname', 'email', 'phone', 'gender', 'dob', 'religion', 'mtn', 'country', 'city', 'district', 'upozila', 'password'];
//only the field names inside the array can be mass-assign
}
更多详细信息:您可以阅读我的答案,并且可以在此处轻松理解“质量分配”在Laravel(Link)中的含义
答案 1 :(得分:0)
这是Laravel的保护机制, 在这里阅读更多内容:
https://laravel.com/docs/5.7/eloquent#mass-assignment
使用create时,请确保模型具有“ $ fillable”属性,如下所示:
protected $fillable = [
'fname',
'lname',
'email',
'phone',
'gender',
'dob',
'religion',
'mtn',
'country',
'city',
'district',
'upozila',
'password'
];
此外,请注意,在将密码存储到数据库之前,应先对密码进行哈希处理。