Mustafa Azhari Mustafa Azhari - 3 months ago 11
PHP Question

How to get a model in controller laravel 5.2

I'm new in laravel and i'm trying to get model post from view in controller method. I used Collective\Html\FormFacade for a view. My view code is below;

{!! Form::model($employee,array("url" => "employee/edit" , "method"=>"POST")) !!}
<ul>
<li>
{{ Form::label('Name', 'Name') }}
</li>
<li>
{{ Form::text('Name') }}
</li>
</ul>
<ul>
<li>
{{ Form::label('Job', 'Job') }}
</li>
<li>
{{ Form::text('Job') }}
</li>
</ul>
<ul>
<li>
{{ Form::label('Salary', 'Salary') }}
</li>
<li>
{{ Form::text('Salary') }}
</li>
</ul>
{{ Form::submit('Update Employee!') }}
{!! Form::close() !!}


My question is how can i get this post data as model in controller ?

Rob Rob
Answer

The easiest way is to instantiate a new model with it.

$employee = new Employee($request->all())

If you are updating the record then retrieve it from the database using the id

public function update(Request $request, $id) {
    $employee = Employee::find($id);
    $employee->fill($request->all());
    $employee->save();
    // ...
}

I suggest reading https://laravel.com/docs/5.2/eloquent#basic-updates

Comments