Property [X] does not exist on this collection instance laravel relationship
The problem is this line:
{{ $phonebook->client->title}}
In your view.
You've setup your relationship as a hasMany
relationship, which will return a collection of models.
If you do dd($phonebook->client)
, it'll return a collection, not a single model.
It's trying to call the property title
on a collection object, not a model.
You need to change the relationship definiation to a hasOne()
, OR do something like:
{{ $phonebook->client->first()->title }}
(or alternatively):
{{ $phonebook->client->get(0)->title }}
Farshad
Updated on June 14, 2022Comments
-
Farshad almost 2 years
i am using has many realtions in laravel 5.6 and when i dd the $phonebooks i see all the relations are working properly and every thing is fine but when i try to show them in view i get the error of property does not exist on this collection here is the relation code
public function client() { return $this->hasMany('App\Client','id' , 'client_id'); }
and here is controller
public function index() { $phonebooks = Phonebook::with('client')->get(); return view('admin.phonebooks.index',compact('phonebooks',$phonebooks)); }
and finally here is how i try to show them in view
<tbody> @foreach($phonebooks as $phonebook) <tr> <th scope="row">{{$phonebook->id}}</th> <th scope="row">{{$phonebook->title}}</th> <td><a href="/admin/phonebooks/{{$phonebook->id}}">{{$phonebook->description}}</a></td> <td>{{$phonebook->calldate}}</td> <td>{{$phonebook->created_at->toFormattedDateString()}}</td> <td>{{ $phonebook->client->title}}</td> <td> <div class="btn-group" role="group" aria-label="Basic example"> <a href="{{ URL::to('admin/phonebooks/' . $phonebook->id . '/edit') }}"> <button type="button" class="btn btn-warning">ویراییش</button> </a> <form action="{{url('admin/phonebooks', [$phonebook->id])}}" method="POST"> <input type="hidden" name="_method" value="DELETE"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="submit" class="btn btn-danger" value="حذف"/> </form> </div> </td> </tr> @endforeach </tbody>
and here is the result of dd just a part of it
Collection {#630 ▼ #items: array:3 [▼ 0 => Phonebook {#572 ▼ #fillable: array:5 [▶] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:8 [▶] #original: array:8 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: array:1 [▼ "client" => Collection {#627 ▼ #items: array:1 [▼ 0 => Client {#621 ▼ #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:16 [▼ "id" => 1
and is goes down just like this .
-
N Mahurin almost 6 years$phonebook->Client->title, shouldn't the c be lower case?
-
Admin almost 6 yearstry client not Client
-
Farshad almost 6 yearsit is lower case in my code sorry my mistake to type here but tried both and no luck
-
Phil Cross almost 6 yearstry replacing
compact('phonebooks', $phonebooks)
with justcompact('phonebooks')
-
Farshad almost 6 years@PhilCross just did it now and no luck yet the same error :(
-
-
Farshad almost 6 years{{ $phonebook->client->first()->title }} this just did the trick for me , how i really dont know i try ro read your answer more and more and more and even more to understand what is going on there :) cause laravel didnt just mentioned that in hasMany realtion you will get something like collection object :) thanks to you u saved my day ;)
-
Phil Cross almost 6 yearsAny relationship that has
Many
in it's name, for example,hasMany
orbelongsToMany
will always return a collection object. When you define the relationship, for example, your phonebooks / client relationship, read it asPhonebook hasMany Client
, orClient belongsTo Phonebook
. -
Farshad almost 6 yearsoh i figured out :) that was easy :P thanks you phil you helped me alot
-
Pol Lluis almost 6 yearsYou could also do something like this which may be more convenient: 1.Change your function name to->clients(), then 2. in your view you can do this-> foreach($phonebook->clients as $client) : //access the client endforeach;