3 namespace BookStack\Entities\Models;
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Database\Eloquent\Scope;
8 use Illuminate\Database\Query\JoinClause;
10 class EntityScope implements Scope
13 * Apply the scope to a given Eloquent query builder.
15 public function apply(Builder $builder, Model $model): void
17 $builder = $builder->where('type', '=', $model->getMorphClass());
18 if ($model instanceof Page) {
19 $builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', 'entities.id');
21 $builder->leftJoin('entity_container_data', function (JoinClause $join) use ($model) {
22 $join->on('entity_container_data.entity_id', '=', 'entities.id')
23 ->where('entity_container_data.entity_type', '=', $model->getMorphClass());