Scopes
Place all scopes in your domain in
Models\Scopesnamespace and always useScopesuffix.
✓ Extend AbstractScope instead of Scope interface
Relation scope
Name your scope
With{RelationName}Scopeand extend AbstractNestedRelationScope. Place the scope where the "relation" model lives.
AbstractNestedRelationScope helps you to make reusable relation scopes with ability to apply scopes on the
relation. Allows you to load a relation and select specified columns, load more relations and others using scope
chaining.
The scope works in 2 modes:
- (default) loads the relation (uses
with) - (pass
useHas: trueconstructor parameter) filters by the relation (useswhereHas)
Create a scope that can be used with a model that has type relation in the model.
<?php
declare(strict_types=1);
namespace App\Object\Models\Scopes;
use LaraStrict\Database\Scopes\AbstractNestedRelationScope;
class WithObjectScope extends AbstractNestedRelationScope
{
protected function getRelationName(): string
{
return 'object';
}
}
Use the scope in your Query
new WithObjectScope(relationScopes: [
new SelectScope(['id', 'object_name']),
])
This example load
objectrelation and selects given attributes.
new WithObjectScope(
relationScopes: [
new WhereProviderIds([$providerId]),
],
useHas: true
),
This example filters entries by given object relation that has given provider_id attribute set to given value-