]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/EntityScope.php
Merge pull request #5917 from BookStackApp/copy_references
[bookstack] / app / Entities / Models / EntityScope.php
1 <?php
2
3 namespace BookStack\Entities\Models;
4
5 use Illuminate\Database\Eloquent\Builder;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Database\Eloquent\Scope;
8 use Illuminate\Database\Query\JoinClause;
9
10 class EntityScope implements Scope
11 {
12     /**
13      * Apply the scope to a given Eloquent query builder.
14      */
15     public function apply(Builder $builder, Model $model): void
16     {
17         $builder = $builder->where('type', '=', $model->getMorphClass());
18         $table = $model->getTable();
19         if ($model instanceof Page) {
20             $builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', "{$table}.id");
21         } else {
22             $builder->leftJoin('entity_container_data', function (JoinClause $join) use ($model, $table) {
23                 $join->on('entity_container_data.entity_id', '=', "{$table}.id")
24                     ->where('entity_container_data.entity_type', '=', $model->getMorphClass());
25             });
26         }
27     }
28 }