Чтобы создать папку с автоматическим путем к образу в Laravel 8, вы можете использовать различные методы. Вот несколько примеров:
Метод 1: использование метода store()с настраиваемым путем
use Illuminate\Support\Facades\Storage;
public function storeImage(Request $request)
{
$image = $request->file('image');
$path = $image->store('images', 'public'); // 'images' is the folder name
// You can also generate a unique filename
// $filename = uniqid() . '.' . $image->getClientOriginalExtension();
// $path = $image->storeAs('images', $filename, 'public');
// Your code to save the path to the database or perform other operations
// ...
return $path; // Returns the path where the image is stored
}
Метод 2: использование метода storeAs()с собственным путем и именем файла
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
public function storeImage(Request $request)
{
$image = $request->file('image');
$filename = Str::random(20) . '.' . $image->getClientOriginalExtension();
$path = $image->storeAs('images', $filename, 'public'); // 'images' is the folder name
// Your code to save the path to the database or perform other operations
// ...
return $path; // Returns the path where the image is stored
}
Метод 3. Использование пользовательского пути на основе текущей даты
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
public function storeImage(Request $request)
{
$image = $request->file('image');
$folderPath = 'images/' . Carbon::now()->format('Y/m/d');
$path = $image->store($folderPath, 'public');
// Your code to save the path to the database or perform other operations
// ...
return $path; // Returns the path where the image is stored
}
Это всего лишь несколько примеров того, как можно создать папку с автоматическим путем к изображению в Laravel 8. Вы можете выбрать метод, который соответствует вашим требованиям, и при необходимости настроить его дополнительно.