Closed jameswong3388 closed 2 years ago
@rahulhaque, but I want to use spatie's laravel-medialibrary
to handle the medias.
How can I integrate with that?
@rahulhaque, I have disabled soft delete
in the config file and nothing changed.
And I updated the handle
method
public function handle(): void
{
$showcase = new Showcase([
"uuid" => $this->uuid->toString(),
"title" => $this->title,
"description" => $this->description,
"slug" => Str::slug($this->title),
]);
$showcase->authoredBy($this->author);
$showcase->save();
$showcase_cover_image = Filepond::field($this->showcase_cover_image)->getModel();
$showcase_images = Filepond::field($this->showcase_images)->getModel();
dump($showcase_cover_image->filepath);
if ($this->showcase_cover_image) { // request->showcase_cover_image
try {
$showcase->addMedia($showcase_cover_image->filepath)->toMediaCollection("showcase_cover_image");
} catch (\Exception $e) {
dump($e);
}
$showcase_cover_image->delete();
}
if ($this->showcase_images) { // request->showcase_images
try {
$showcase->addMedia($showcase_cover_image->filepath)->toMediaCollection("showcase_images");
} catch (\Exception $e) {
dump($e);
}
$showcase_images->delete();
}
}
@jameswong3388 Filepond::field($this->showcase_cover_image)->delete()
and Filepond::field($this->showcase_images)->delete()
. This method is for instantly deleting the temporary file. However, you can make use of filepond:clear
command in schedule.
@rahulhaque, no problem I have got the problem solved 😂
@jameswong3388 people tend to skip reading documentation. For spatie’s media library that works with laravel file object, getFile()
method of this package should be useful but least used. Close the issue if done. Attach your implementation of media library for others if possible.
@rahulhaque, sure !
For those who want to integrate spatie's laravel-medialibrary
, here is an example.
public function handle(): void
{
$showcase = new Showcase([
"uuid" => $this->uuid->toString(),
"title" => $this->title,
"description" => $this->description,
"slug" => Str::slug($this->title),
]);
$showcase->authoredBy($this->author);
$showcase->save();
$showcase_cover_image = Filepond::field($this->showcase_cover_image)->getModel(); // single upload
$showcase_images = Filepond::field($this->showcase_images)->getModel(); // chunk upload
// request()->showcase_cover_image
if ($this->showcase_cover_image) {
try {
$showcase->addMediaFromDisk($showcase_cover_image->filepath, 'local')
->toMediaCollection("showcase_cover_image");
} catch (\Exception $e) {
dump($e);
}
Storage::disk('local')->delete($showcase_cover_image->filepath);
$showcase_cover_image->forceDelete();
}
// request()->showcase_images
if ($this->showcase_images) {
try {
foreach ($showcase_images as $showcase_image) {
$showcase->addMediaFromDisk($showcase_image->filepath, 'local')
->toMediaCollection("showcase_images");
}
} catch (\Exception $e) {
dump($e);
}
foreach ($showcase_images as $showcase_image) {
Storage::disk('local')->delete($showcase_image->filepath);
$showcase_image->forceDelete();
}
}
}
I had a job to create a "post", after the post is created all the temporary files should be deleted. But weird things happened, pls look at the codes.