oxbir
1 month ago
380
14
Laravel

How to storage upload an image and save in database?

Posted 1 month ago by oxbir

Why this code does not work?

public function store(CategoryRequest $request)
{
    $path = null;
    if ($request->hasFile('image')) {
        $file = $request->file('image');
        $nane = time();
        $extension = $request->file('image')->getClientOriginalExtension();
        $filename = $nane . '.' . $extension;
        $path = $request->file('image')->storeAs('images/categories/', $filename);
        $path = Storage::putFileAs('images/categories', $request->file('image'), $filename);
    }
    Category::create([
        'name' => $request->name,
        'latin' => $request->latin,
        'parent_id' => $request->parent_id,
        'image' => $path,
    ]);
    return redirect()->route('categories.index');
}

I added enctype="multipart/form-data" in my form tag now.

My code is does not uploads and does not save in database.

I tried in 10 hours but I did not solve my problem.

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