Ezra
Ezra

Reputation: 143

Error updating data from form in laravel 5.2

I want to update my data using this form, however I am getting an error:

The webform my controller:

public function update($id)
{
    $dosenUpdate = Request::all();
    $dosen = Dosen::find($id);
    $dosen->update($dosenUpdate);
    return redirect('dosen')->with('message', 'Data berhasil dirubah!');
}

public function status()
{
    $dosen = \App\Dosen::paginate(5);
    return view('dosen.status', compact('dosen'));
}

my route:

Route::get('/dosen/status', 'DosenController@status');

my view:

{!! Form::model($dosen, ['route' => ['dosen.update', $dosen->id] !!}
 {!! Form::hidden('_method', 'PUT') !!}
 {!! Form::select('status', array('1' => 'Ready', '0' => 'Not Ready'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control'], ['placeholder' => 'Pilih Status']) !!}
 {{ Form::button('<i class="fa fa-check-square-o"></i> Save', ['type' => 'submit', 'class' => 'btn btn-primary'] )  }}
{!! Form::close() !!}

The error response:

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View:       
D:\XAMPP\htdocs\infodosen\resources\views\dosen\status.blade.php)

How do I fix this?

Upvotes: 0

Views: 101

Answers (1)

RDev
RDev

Reputation: 1281

You are triyng to get a property from a collection of objects ( LengthAwarePaginator ).

To get the id of model Dosen in your view, you must iterate the collection.

Something like this:

@foreach($dosen as $d)
  {!! Form::model($d, ['route' => ['dosen.update', $d->id] !!}
    {!! Form::hidden('_method', 'PUT') !!}
    {!! Form::select('status', array('1' => 'Ready', '0' => 'Not Ready'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control'], ['placeholder' => 'Pilih Status']) !!}
    {{ Form::button('<i class="fa fa-check-square-o"></i> Save', ['type' => 'submit', 'class' => 'btn btn-primary'] )  }}
  {!! Form::close() !!}
@endforeach

If you get a TokenMismatch error, be sure to include your routes inside the Route group with the middleware called 'web'.

For example:

Route::group(['middleware' => ['web']], function () {
//put your routes here
}

This will take care also of the error where your $error variable is unset inside your views

Upvotes: 1

Related Questions