Property [X] does not exist on this collection instance laravel relationship

10,245

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 }}
Share:
10,245
Farshad
Author by

Farshad

Updated on June 14, 2022

Comments

  • Farshad
    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>&nbsp;
                    <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
      N Mahurin almost 6 years
      $phonebook->Client->title, shouldn't the c be lower case?
    • Admin
      Admin almost 6 years
      try client not Client
    • Farshad
      Farshad almost 6 years
      it is lower case in my code sorry my mistake to type here but tried both and no luck
    • Phil Cross
      Phil Cross almost 6 years
      try replacing compact('phonebooks', $phonebooks) with just compact('phonebooks')
    • Farshad
      Farshad almost 6 years
      @PhilCross just did it now and no luck yet the same error :(
  • Farshad
    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
    Phil Cross almost 6 years
    Any relationship that has Many in it's name, for example, hasMany or belongsToMany will always return a collection object. When you define the relationship, for example, your phonebooks / client relationship, read it as Phonebook hasMany Client, or Client belongsTo Phonebook.
  • Farshad
    Farshad almost 6 years
    oh i figured out :) that was easy :P thanks you phil you helped me alot
  • Pol Lluis
    Pol Lluis almost 6 years
    You 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;