Ad
Laravel Scopes With Relation And Conditions
i have this laravel scope code
public function apply(Builder $builder, Model $model)
{
$builder->whereDoesntHave('getPermissionData');
}
everything working fine till now what i need is to wright conditions like this
public function apply(Builder $builder, Model $model)
{
/*
if($builder->whereHas('getPermissionData'))
{
check the relation
and do some code here
}
*/
$builder->whereDoesntHave('getPermissionData');
}
is that possible or not
Ad
Answer
You need to create two different conditions in or
with each others. You also should wrap this condition in a where
clause so next conditions won't be in or
aswell
public function apply(Builder $builder, Model $model) {
$builder->where(function ($query) {
$query->whereDoesntHave('getPermissionData')
->orWhereHas('getPermissionData', function ($permissionQuery) {
// Apply your condition to the relation query
});
});
}
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad