Answer the question
In order to leave comments, you need to log in
How to delete a file in laravel?
The file path in the database is stored in this form -
/storage/img/COwpwZLq3PeFO3TdxK4EVXQ8EGo77vATZZEi8VHC.jpeg
and for herStorage::delete('/storage/img/COwpwZLq3PeFO3TdxK4EVXQ8EGo77vATZZEi8VHC.jpeg')
does not work, in delete you need to specify the path of this formatpublic/img/COwpwZLq3PeFO3TdxK4EVXQ8EGo77vATZZEi8VHC.jpeg
$path = Storage::putFile('public/img', $request->file('img'));
$url = Storage::url($path);
$post->img = $url;
Answer the question
In order to leave comments, you need to log in
Reading the documentation and learning good practice makes life a little easier.
So.
Storing url in the database, as you do, is usually not profitable and uninteresting. This is necessary in exceptional cases of a one-time mini-project, when you know for sure the settings will not change and the files will not be edited. Or, in specialized databases for caching the result of url generation.
In other cases, you need to store the file name in the database. Ideally, split the file name, but in most cases two fields will suffice: name with path + drive name. This will close most cases for future scaling. But if you're really too lazy, then the drive name can also be omitted, although I advise you to add this field.
Next, your code:
$path = Storage::putFile('public/img', $request->file('img'));
$url = Storage::url($path);
$post->img = $url;
// file('img') - это имя файла(инпута формы например) из запроса
// store('img') - это имя подпапки для
// 'public' - Диск из настроек (см filesystems.php)
$path = $request->file('img')->store('img', 'public');
$post->img = $path;
/storage/app/
(I put a slash at the beginning to show that the path is from the root of the folder with the project) Storage::delete($post->img);
$url = asset($post->img);
<img src="{{asset($post->img)}}">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question