/** * @param \Illuminate\Database\Eloquent\Builder|Builder $query * @param string|\Atrauzzi\Phperclip\Model\Clippable $clippableOrType * @param int|string $clippableId * @return \Illuminate\Database\Eloquent\Builder * @throws \Exception */ public function scopeForClippable(Builder $query, $clippableOrType, $clippableId = null) { if ($clippableOrType instanceof Clippable) { $clippableType = $clippableOrType->getMorphClass(); $clippableId = $clippableOrType->getKey(); } elseif ($clippableOrType && $clippableId) { $clippableType = $clippableOrType; } else { throw new Exception(sprintf('Invalid clippable type %s/%s.', $clippableOrType, $clippableId)); } return $query->where('clippable_type', $clippableType)->where('clippable_id', $clippableId); }
/** * Only returns FileMeta instances that are clipped to a Clippable * * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $builder * @param \Atrauzzi\Phperclip\Model\Clippable|string $clippableOrType * @param null|int|string $clippableId * @return \Illuminate\Database\Eloquent\Builder * @throws \Exception */ public function scopeClippedTo(Builder $builder, $clippableOrType, $clippableId = null) { $builder = $builder->join('phperclip_clipping', 'phperclip_clipping.file_meta_id', '=', 'phperclip_file_meta.id'); if ($clippableOrType instanceof Clippable) { $clippableType = $clippableOrType->getMorphClass(); $clippableId = $clippableOrType->getKey(); } elseif ($clippableOrType && $clippableId) { $clippableType = $clippableOrType; } elseif ($clippableOrType) { throw new Exception(sprintf('Invalid clippable type %s/%s.', $clippableOrType, $clippableId)); } if (!empty($clippableType) && !empty($clippableId)) { $builder = $builder->where('phperclip_clipping.clippable_type', $clippableType)->where('phperclip_clipping.clippable_id', $clippableId); } return $builder; }
/** * Associate a FileMeta to a Clippable * * @param \Atrauzzi\Phperclip\Model\FileMeta $fileMeta * @param \Atrauzzi\Phperclip\Model\Clippable $clippable * @param null|string $slot */ public function attach(FileMeta $fileMeta, Clippable $clippable, $slot = null) { $clippable->files()->create(['file_meta_id' => $fileMeta->getKey(), 'slot' => $slot]); }