O
O
OlegSedoy2020-09-08 04:05:10
Laravel
OlegSedoy, 2020-09-08 04:05:10

How to delete a file in spatie/laravel-medialibrary?

I get a list of files associated with the model:

@if($share->hasMedia('shares'))
            <div class="form-group">
                <label>Загруженные изображения</label>
                @foreach($share->getMedia('shares') as $media)
                    <div class="card" style="width: 10rem;">
                        <img class="card-img-top" src="{{$media->getUrl()}}">
                        <a class="btn btn-block btn-danger rounded-0"
                           href="{{route('delete.media',['id'=> $media->id])}}">удалить</a>
                    </div>
                @endforeach
            </div>
        @endif


The template has a link to delete a file, controller code
class mediaDeleteController extends Controller
{

    public function __invoke(Request $request)
    {

        Media::whereIn('id', [$request->id])->delete();

        return back();
    }
}


The problem is that only the link to the model is removed, the physical file still remains.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mokhirjon Naimov, 2020-09-08
@OlegSedoy

I think it is necessary to delete through the model, and not through the query builder. That is like this:
Media::findOrFail($request->id)->delete();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question