Home > Back-end >  How to save path after upload in laravel storage?
How to save path after upload in laravel storage?

Time:07-18

I want to after uploading an image save it like this

images/blogs/1650953308.jpg

But I see this

1650953308.jpg

I want to save like this

images/blogs/1650953308.jpg

public function store(Request $request)
{
    $fileNameService = $request->file('image') ?? null;
    if ($request->hasFile('image'))
    {
        $file = $request->file('image');
        $name = time();
        $extension = $file->getClientOriginalExtension();
        $fileNameService = $name . '.' . $extension;
        $img = Image::make($file->path());
        $img->resize(600, 300, function ($constraint) {
            $constraint->aspectRatio();
        })->save('storage/images/blogs/'.$fileNameService);
    }
    Blog::query()->create([
        'image' => $fileNameService,
    ]);
    return redirect()->route('admin.blogs.index');
}

CodePudding user response:

Concatenate folder path with image name.

 Blog::query()->create([
        'image' => 'images/blogs/'.$fileNameService,
    ]);

It will save as images/blogs/1650953308.jpg.

  • Related