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

dzonkile

I have three tables('users,cars and photos').

users table

enter image description here

photos table

enter image description here

I want to display the newest image file for user avatar picture. In this case I want to show as profile avatar photo with id = 2 because it's the latest photo for the user(id=1) because of imageable_id=1 and because imageable_type is for User(for user avatar). App\Models\Car belongs to cars and I don't need that for now.

Summary: Want to display the newest photo for the user avatar.

I Am using this code below inside my blade file:

<img src="{{$detected_user->photo->file}}" alt="">

In Controller I use $detected_user to authenticate user which is logged in and I use '->photo'(relationship inside my model). '->file' is the name of the column inside my 'photos' table.

User Model

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

Cars Model

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

Photo Model

public function imageable() {
    return $this->morphTo();
}
Donkarnash

On the User model you can define two relationships

//App\Models\User.php

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

public function latest_photo()
{
    return $this->morphOne('App\Models\Photo', 'imageable')->latest('id');
}

In the view

<img src="{{$detected_user->latest_photo->file}}" alt="">

And similarly for the Car model

//App\Models\Car.php

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

public function latest_photo()
{
    return $this->morphOne('App\Models\Photo', 'imageable')->latest('id');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Property [name] does not exist on this collection instance in Laravel 6

Property [apps] does not exist on this collection instance

Property [title] does not exist on this collection instance

Property [id] does not exist on this collection instance

Property [id] does not exist on this collection instance in laravel 5.5

Property [customInput] does not exist on this collection instance

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

Property [bId] does not exist on this collection instance

Laravel: Property [name] does not exist on this collection instance

Property [***] does not exist on this collection instance Laravel eloquent relationship

Property [owner] does not exist on this collection instance

laravel collection/format printing |Property [id] does not exist on this collection instance

Property [TitleEn] does not exist on this collection instance

Laravel - Iterating pivot data (Property does not exist on this collection instance)

Property [client] does not exist on this collection instance

"Property [body] does not exist on this collection instance

Error: Property [tit] does not exist on this collection instance - Laravel

Laravel - Property does not exist on this collection instance

Property [users] does not exist on this collection instance

Error "Property [nom] does not exist on this collection instance" in Laravel

Property [associated_occupation] does not exist on this collection instance. - Laravel

laravel 5.5 Property [libros] does not exist on this collection instance

Laravel 6 relationships: Property [does not exist on this collection instance

Laravel - Property [comment] does not exist on this collection instance

Property [id] does not exist on this collection instance - Laravel 6

Laravel Relationship Property Does Not Exist In This Collection Instance

Laravel - Property [filieres] does not exist on this collection instance

Property [image] does not exist on this collection instance. / Laravel - with() Method

property [hadir] does not exist on this collection instance

TOP Ranking

HotTag

Archive