Scopes
Place all scopes in your domain in
Models\Scopes
namespace and always useScope
suffix.
✓ Extend AbstractScope
instead of Scope
interface
Relation scope
Name your scope
With{RelationName}Scope
and 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: true
constructor 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
object
relation 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-