P
P
part_os2019-10-03 23:42:33
Laravel
part_os, 2019-10-03 23:42:33

How to apply global stubs to lazy loading?

Hello, tell me how to apply the global preparation to lazy loading?
We have a model

Region::with('city')->get();
или 
Region::all();

public function city()
    {
        return $this->belongsToMany('App\City','region_city','region_id','city_id');
    }

global procurement
class AccountScopes implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('account_id', '=',  1);
    }
}

We get the first request using the workpiece, but if I get the associated model, it does not apply to these requests.
select * from `regions` where `account_id` = 1

select `citys`.*, `regions`.`region_id` as `pivot_region_id`, `region_city`.`city_id` as `pivot_gorodid` from `city` inner join `region_city` on `city`.`id` = `region_city`.`city_id` where `region_city`.`region_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

If the all() method is called and the ->city is received as an associated model, it does not substitute either.
How can this be decided?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
part_os, 2019-10-04
@part_os

I found the reason, it turned out that in the linked table it is also necessary to apply global blanks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question