How to store upload an image?

Posted 1 month ago by irankhosravi

I want to store a product and upload an image?

I don,t want to rename it in database

For example:

I have an image, the image name is 1.jpg

When I upload an image 1.jpg, but It save to database rename it

Upload

ProductController

public function store(ProductRequest $request)
{
    if ($request->hasFile('images')) {
        $fileNameWithExt = $request->file('images')->getClientOriginalName();
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        $extention = $request->file('images')->getClientOriginalExtension();
        $fileNameToStore = $filename.'_'.time().'.'.$extention;
        $path = $request->file('images')->storeAs('public/images', $fileNameToStore);
    }
    $product = auth()->user()->products()->create(array_merge($request->all(), ['images' => $path]));
    $product->categories()->attach($request->category);
    return redirect()->route('products.index');
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.