Boubou
1 month ago
380
3
Laravel

Accessing relationships from another relationship, how?

Posted 1 month ago by Boubou

Hello,

Here is what I have:

Model USER:

    public function ads()
    {
        return $this->hasMany(Listing::class);
 
    }

Model AD:

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

MODEL Category

    public function ads()
    {
        return $this->hasMany(Listing::class, 'category_one_id');
    }

How can I call the user, get all his ads and the category linked to each ad please? I am confused.

Thank you.

Please sign in or create an account to participate in this conversation.