/** * Includes the required relations in the query builder. * * @todo Find a way to apply accessors through this method. * * @param Illuminate\Database\Eloquent\Builder $query * @param string|array|Illuminate\Support\Collection $embed * @return Illuminate\Database\Eloquent\Builder */ public function scopeEmbed($query, $embed) { // Relations and accessors to append. $separator = isset($this->embedSeparator) ? $this->embedSeparator : ','; $attributes = is_string($embed) ? @explode($separator, $embed) : (array) $embed; // Extract the accessors from the list of attributes. $embedable = isset($this->embedable) ? $this->embedable : []; $accessors = array_intersect(array_keys($embedable), $attributes); // Extract the relations from the list of attributes. $relations = array_filter(array_diff($attributes, array_keys($embedable))); // If the accessors require any relation, add them to the list. foreach ($accessors as $accessor) { // Performance check. if (empty($embedable[$accessor])) { continue; } $relations = array_merge($relations, $embedable[$accessor]); } // Remove duplicates. $relations = array_unique($relations); // TODO: Temporarily set "appends" array to embedable accessors. return $query->with($relations); }
/** * Fetches title with relations by id. * * @param Illuminate\Database\Eloquent\Builder $query * @param Int $id * @return collection */ public function scopeById($query, $id) { return $query->with('Actor', 'Image', 'Director', 'Writer', 'Review', 'Season.Episode')->findOrFail($id); }