for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Thinktomorrow\AssetLibrary\Application;
use Thinktomorrow\AssetLibrary\HasAsset;
class DetachAsset
{
/**
* Detaches an asset from a model.
*
* @param $ids
*/
public function detach(HasAsset $model, $ids, $type, $locale): void
if (! is_array($ids)) {
$ids = (array) $ids;
}
$ids = $this->ensureParameterIsString($ids);
foreach ($ids as $id) {
$model->assetRelation()->where('asset_pivots.type', $type)->where('asset_pivots.locale', $locale)->detach($id);
* Detaches all assets or for a specific type from a model.
public function detachAll(HasAsset $model, ?string $type = null): void
$assetIds = $type
? $model->assetRelation()->where('asset_pivots.type', $type)->get()->pluck('id')
: $model->assetRelation()->get()->pluck('id');
$model->assetRelation()->detach($assetIds);
* @param mixed $ids
* @return string[]
public function ensureParameterIsString(mixed $ids): array
return array_map(fn($id) => (string)$id, $ids);