-2

I have three tables(users, cars and pivot table photos) and many to many polymorphic relationship between them..

photos table

enter image description here

I have collection of photos and when I want to display $car->photo->file I am getting this error:

Property [file] does not exist on this collection instance.

I am calling inside blade file {{$car->photo->file}} $car is variable of $cars which I foreach from my controller, ->photo is the name of relationship between car and photo and ->file is the name of the column inside photos table.

My User model: this is for images which are related to profile avatars

public function photo() {
    return $this->morphMany('App\Models\Photo', 'imageable');
}

My Car model:

public function photo() {
    return $this->morphMany('App\Models\Photo', 'imageable');
}

My Photo model:

public function imageable() {
    return $this->morphTo();
}
dzonkile
  • 13
  • 5
  • 1
    show the code that is actually causing the error and how you get the data you are trying to access this property on .... but `morphMany` will return a Collection not a single model; collections don't have these properties, the models that are contained in them do ... so `photo` is a bad name, it would be `photos` since it isn't a singular relationship (less confusing) – lagbox May 31 '22 at 16:57
  • A `Collection` is not an instance of a single Model. `{{ $car->photo }}` returns **multiple** `Photo` model instances, so `{{ $car->photo->file }}` will not work; which `->file` are you trying to reference? `{{ $car->photo->first()->file }}` can work, assuming the `$car` has associated `Photo` models, but ultimately it is up to you to handle this properly. – Tim Lewis May 31 '22 at 17:00
  • Also, `photo` is a singular noun; that should be renamed to `photos`, the plural, to properly convey what is being represented. You wouldn't call a variable `$cars` if it represented a single `Car` model; you'd call it `$car`. The same rule should apply to method/relationship names. (Lagbox also pointed this out, didn't see the edit lol) – Tim Lewis May 31 '22 at 17:01
  • I have User which have avatar and that photo is stored inside photos table. Also that User can post car ad and that ad contains car image which is also stored inside photos table. Imageable_type is difference between that. User avatar great super because I used $user->photo->latest('id')->file but for cars I can't solve – dzonkile May 31 '22 at 17:04
  • In my controller I have $cars = Car::all(). Then, I compact it to blade file. Also then I foreach that: $cars as $car and theeen I am trying to display photo for that car by using {{$car->photo->file}} – dzonkile May 31 '22 at 17:06
  • 1
    again .... `photo` is the dynamic property for the Morph **Many** relationship which returns MANY not 1 ... Collections dont have a `file` property, your Model does, The Collection contains many Models you would have to get that property from ONE of those Models in the Collection ... you are using the wrong relationship as you don't want MANY you only want ONE it would seem, just based on the name `photo` not `photos` – lagbox May 31 '22 at 17:13
  • so how Can i display image per each car ad? – dzonkile May 31 '22 at 17:14
  • Can I have one photos table for users avatars and second one for cars images? – dzonkile May 31 '22 at 17:19
  • 1
    I said in my comment already that `{{ $car->photos->first()->file }}` could work, assuming each `$car` has at least 1 associated image. (assuming you also rename `public function photo()` to `public function photos()`). Beyond that, you'd have to iterate: `@foreach ($car->photos as $photo) {{ $photo->file }} @endforeach` – Tim Lewis May 31 '22 at 17:22

0 Answers0