Undefined property: Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4

I am using laravel v 4.2 .. I want to create an update record. can you help me .. what's wrong with this code ... this is my code:

MatakuliahsController.php

  public function edit ($ id)
     {
         // $ matakuliahs = $ this-> matakuliahs-> find ($ id);
         $ matakuliahs = Matakuliah :: where ('id', '=', $ id) -> get ();

         if (is_null ($ matakuliahs)) {
             return Redirect :: route ('matakuliahs.index');
         }

         return View :: make ('matakuliahs.edit', compact ('matakuliahs'));
     }

edit.blade.php

  {{Form :: open (array ('autocomplete' => 'off', 'method' => 'PATCH', 'route' => array ('matakuliahs.update', $ matakuliahs-> id )))}}
 ...
 {{Form :: close ()}}

Mistake:

  Undefined property: Illuminate \ Database \ Eloquent \ Collection :: $ id (View: C: \ xampp \ htdocs \ Laravel 4 \ projectLaravel \ app \ views \ matakuliahs \ edit.blade.php)

Thank you for your attention and your help.

+9
collections properties php undefined
source share
4 answers

What you are trying to get is an attitude towards a set of models; relationships exist on an object in this collection. You can use first () to return the first, or you need to use a loop for each of them to get its elements.

$matakuliahs = Matakuliah::where('id','=',$id)->get()->first(); 
+25
source share
 $matakuliahs = Matakuliah::where('id','=',$id)->get(); 

returns a collection of the object where the identifier is $ id. in this case it will return a collection of 1 element, not the object itself, if the identifier is unique, so when you do:

 $matakuliahs->id 

you are trying to access the id properties of the $ matakuliahs object , but $ matakuliahs is not a collection in this case. To solve this problem you can do:
one.

 $matakuliahs = Matakuliah::where('id','=',$id)->get()->first(); 

or

 $matakuliahs = Matakuliah::where('id','=',$id)->first(); 

to get an object and access properties.

2. in your opinion:

 @foreach( $matakuliahs as $matakuliah) //your code here @endforeach 

hope this helps. thanks

+4
source share

Try this in the controller method:

 $matakuliahs = Matakuliah::find($id); 

And just pass it on to the view.

0
source share

Method

 MyModel::find($id); 

Or a relationship with Eloquent doesn't work on Laravel 4.2, just the following solution

 $myModel= new MyModel; $myModel->setConnection('mysql2'); $myModel= $myModel->where('id', $id)->first(); 
0
source share

All Articles