Joco25 Joco25 - 1 year ago 76
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 (

) 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: "",
**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();


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']) !!}

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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download