Support the ongoing development of Laravel.io →
Database Eloquent

Hello guys,

I'm trying to show featured listings from sub categories in a category view.

I can check children categories and then get featured listings in each child category. But it does not seem to a good method. Could somebody please give me a hint if I can do it with eloquent relationship?

Last updated 3 years ago.
0

Can you provide your model relationships and which data from each model your looking for

0

Hi RDelorier,

I've a Category and a Listing model

  1. Category with parent id is Sub Category
  2. Each category has some sub categories
public function children(){
        return $this->hasMany('App\Cat','parent_id')->whereStatus(1);
}
  1. (Sub) category hasMany Listings
public function listings(){
        return $this->hasMany('App\Listing')->whereStatus(1);
}

and 4. in Listing model

public function cat(){
        return $this->belongsTo('App\Cat');
}

Now, when I'm in category, I want to fetch all featured items all of its sub categories. Home this makes sense.

Last updated 9 years ago.
0

See ==http://laravel.io/forum/04-04-2015-looping-through-collection. Plus there are Laracast on this.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.