oxbir
1 month ago
302
14
Laravel

How to upload and save image file?

Posted 1 month ago by oxbir

Why this code does not work?

public function store(CategoryRequest $request)
{
    if ($request->hasFile('image')) {
        $randomize = rand(111111, 999999);
        $extension = $request->file('image')->getClientOriginalExtension();
        $filename = $randomize . '.' . $extension;
        $image = $request->file('image')->move('images/categories/', $fileName);
    }
    Category::create([
        'name' => $request->name,
        'latin' => $request->latin,
        'parent_id' => $request->parent_id,
        'image' => $image ?? null,
    ]);
    return redirect()->route('categories.index');
}

This is the code that does not uploads and does not save.

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