]> BookStack Code Mirror - bookstack/blob - app/Entities/Models/EntityScope.php
Images: Added nulling of image page relation on page delete
[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         if ($model instanceof Page) {
19             $builder->leftJoin('entity_page_data', 'entity_page_data.page_id', '=', 'entities.id');
20         } else {
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());
24             });
25         }
26     }
27 }