i have at least 8 components have same relation with course model i want to un return this component if this course is hidden
i tried to make it in global scope but still need to do in all these component's model whereHas and with how can i do these in scope in which model?
i don't want to repeat these relation in all component maybe once in maybe Global Scope or something like that
Note: i worked with laravel 5.8 i must repeat this in all components like material
$callQuery=function($q) use ($request){
if(!$request->user()->can('course/show-hidden-courses'))
$q->where('show',1);
};
// $material = $materials_query->with(['lesson','course.attachment'])->whereIn('lesson_id',$lessons);
$material = $materials_query->whereHas('course',$callQuery)->with(['lesson','course' => $callQuery]);
I am not sure, if I understood your issue well. But, if you are looking for using the same whereHas everywhere. You can just create a scope in your model, basically like:
And andywhere you like to have this filter, you can just query like:
Besides, if you want to enforce it everywhere, you can just create a Global Scope in for example;
App\Model\Scopewith the bellow code:And then in your model, you can just:
And then, you can just write your query like:
The scope will be automatically enforced without actually calling the scope, in our case
course(). There will be times that you won't need the enforce, so you can call like: