Reputation: 61
I try to insert a value into the database by a form in Laravel but my all value's is not inserted.
Insert only email,password,created_date
Here is my code of migration:-
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();
});
Here is my code of controller:-
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',
]);
}
Here is my array = $request->all();
_token "wDRoDeLkOFX5re5nba2Ufv5pr0iKzYVCr0tK9EFE"
fname "nirab"
lname "nirax"
email "[email protected]"
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"
Upvotes: 1
Views: 208
Reputation: 10071
You may use the create
method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable
or guarded
attribute on the model, as all Eloquent models protect against mass-assignment by default.
Fillable you specify which fields are mass-assignable in your model, you can do it by adding the special variable $fillable
to the model. So in the model:
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
}
More details: you can read my answer and you can easily understand here What does “Mass Assignment” mean in Laravel (Link)
Upvotes: 1
Reputation: 1771
It is a protection mechanism of Laravel, Read a bit more here:
https://laravel.com/docs/5.7/eloquent#mass-assignment
When you use create, make sure your model has a "$fillable" property, like so:
protected $fillable = [
'fname',
'lname',
'email',
'phone',
'gender',
'dob',
'religion',
'mtn',
'country',
'city',
'district',
'upozila',
'password'
];
Also, pay attention that your password should be hashed before you store it in your database.
Upvotes: 0