+ $query->leftJoinSub(function (QueryBuilder $query) {
+ $query->select(['id as entity_id', DB::raw("'page' as entity_type"), 'owned_by', 'deleted_at', 'draft'])->from('pages');
+ $tablesByType = ['page' => 'pages', 'book' => 'books', 'chapter' => 'chapters', 'bookshelf' => 'bookshelves'];
+ foreach ($tablesByType as $type => $table) {
+ $query->unionAll(function (QueryBuilder $query) use ($type, $table) {
+ $query->select(['id as entity_id', DB::raw("'{$type}' as entity_type"), 'owned_by', 'deleted_at', DB::raw('0 as draft')])->from($table);
+ });
+ }
+ }, 'entities', function (JoinClause $join) use ($tableName, $entityIdColumn, $entityTypeColumn) {
+ $join->on($tableName . '.' . $entityIdColumn, '=', 'entities.entity_id')
+ ->on($tableName . '.' . $entityTypeColumn, '=', 'entities.entity_type');
+ });
+