use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
}
+ /**
+ * @return Media[]
+ */
public function getAllMedia()
{
return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get();
}
return $f->filterValue($value);
}
+
+ public function replicate(array $except = null)
+ {
+
+ /** @var static $new */
+ $new = parent::replicate($except);
+ $new->push();
+
+ $newId = $new->getIdValue();
+
+ foreach ($this->getAllMedia() as $media) {
+ $cloned = $media->replicate();
+ $cloned->model_id = $newId;
+ $cloned->push();
+ // Copy files
+ $from = public_path('storage/' . $media->id);
+ $to = public_path('storage/' . $cloned->id);
+ if (file_exists($to)) {
+ `rm -rf $to`;
+ }
+ $cp = "cp -R $from $to";
+ `$cp`;
+ }
+
+ return $new;
+ }
}