Joco25 Joco25 - 3 months ago 10
MySQL Question

Laravel persisting data to database as null while return all statement displays correct data

Please I am new to Laravel 5.2, I have a form in which one of my select fields (

role_id
) is populated with data from my database roles table this works just fine. When returning the request and the data is returned perfectly:

{
_token: "lKF7Inzy1sGnQ1DwX6yBZCqDjTpgcvqfBqANuInz",
name: "jingle2",
email: "jingle2@lodx.com",
**role_id: "1",**
is_active: "1",
password: "jocoblinks",
file: { }
}


But upon persisting the data on the form to the database, I in turn get a null value for this field.

Method for persisting data:

public function store(UserRequest $request)
{
//return $request->all();

User::create($request->all());

return redirect('/admin/users');
}


This is what the field on my form looks like:

<div class="form-group">
{!! Form::label('role_id', 'Role:') !!}
{!! Form::select('role_id', [0=>'Choose options'] + $roles, null, ['class'=>'form-control']) !!}
</div>


The
$roles
on the form is gotten from:

public function create()
{
$roles = Role::lists('name', 'id')->all();

return view('admin.users.create', compact('roles'));
}


Please I have tried to debug this but I don't really understand why this is happening.

Answer

You should add role_id column to the fillable variable. To do that, you need open your User model and add this field to protected variable $fillable. You can read about it in documentation: https://laravel.com/docs/5.2/eloquent#mass-assignment

Comments