arctushar's avatar

Relationship in relationsip in relationship with limit

Hi I have a table name and column name given below

  1. sectors = 'id' , 'name'
  2. codelists = 'id' , 'name' , 'sector_id'
  3. datas = 'id', 'codelist_id' , 'trade', 'volume'

In Codelist Model I have 'datas' method

    public function datas()
    {
        return $this->hasMany('App\Data');
    }

in Sector Model I have method

    public function codelists()
    {
        return $this->hasMany('App\Codelist');
    }

Now I want to make a query which will give me

  1. Last 03 Sector name
  2. Correspondence Every sector last 04 codelist
  3. then corresondence every codelist last 05 datas

Is it possible ?? I tried by below

        return $sectors=Sector::select('id','name')->with(array('codelists'=>function($query){
                $query->with(array('datas'=>function($query1){
                    $query1->take(5)->get();
                })
                )->take(4)->get();
            }))->take(3)->get();

Here I found that only for first codelist of first first sector, 05 datas is showing. All other is null array for datas.

can anybody give me solution ?

0 likes
5 replies
mdeorue's avatar

Maybe this is a little better approuch,

return $sectors=Sector::select('id','name')
    ->with([
        'codelists' => function($query){
            $query->latest()->take(4);
        },
        'codelists.data' => function($query){
            $query->latest()->take(5);
        }
    ])->latest()->take(3)->get();

Regards

1 like
Sergiu17's avatar
public function datas()
{
    return $this->hasMany('App\Data')->take(5);
}
public function codelists()
{
    return $this->hasMany('App\Codelist')->take(4);
}
Sector::with(['datas', 'codelists'])->take(3)->get();

I think it should work :)

1 like
amitshahc's avatar

Hi @arctushar, I still can't figure out the solution for this. in eloquent. Does anybody have a working solutions?

the 2 answers given here not working.

1 like
amitshahc's avatar
$user->load(["relation" => function ($qco) use ($max){
            return $qco->select(...)
                ->orderBy('created_at', 'desc')
                ->with(["child" => function ($qtg) {
                    return $qtg->select(...))
                        ->latest()->take(3);
                }])->take($max);

the above generate below sql query:

[{"query":"select * from `relation` where  `relation`.`user_id` in (2)) and `relation`.`deleted_at` is null order by `created_at` desc limit 5","bindings":[],"time":10.4},
{"query":"select * `child` where `child`.`foreign_key` in (2307, 2308, 2312, 2324, 2325) and  `child`.`deleted_at` is null order by `created_at` desc limit 3","bindings":[],"time":3.22}] 

so it's quite obvious the child is sum of total 3 including all relation (2307, 2308, 2312, 2324, 2325) under user.

This is in laravel 8.12. I noticed the above is giving expected result in Laravel 11.

Also mind this link: https://laracasts.com/discuss/channels/eloquent/query-with-relationship-and-limit

you have the reason and answer.

Please or to participate in this conversation.