0
class Products extends Controller
{
    public function index()
{
        $products = DB::table('product')->get();
        return view("product", compact('products'));
}
    public function store(Request $request)
{
            $file = $request->file('image');
        if ($request->hasFile('image')) {
            $name = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $path =  $file->store(public_path('image'));
            $request->image->move($path, $extension);
 }
        DB::table('product')->insert([
            'name' => $request->name,
            'price' => $request->price,
            'photo' => $name
]);
}
}

strong text// //i make correct route and make sure from form inputs and enctype="multipart/form-data" but i have error when use my method store i see error Call to a member function getClientOriginalName() on null and dd{$file} return null

  • Add a `dd($file);` before the if to see what the output contains. – Paul T. May 14 '22 at 00:48
  • To upload a file from Frontend you must use `Content-Type: multipart/form-data;` instead of `enctype="multipart/form-data"` then you will be able to retrieve the file with `$file = $request->file('image');` – Guille May 14 '22 at 02:16
  • Does this answer your question? [Laravel : $request->hasFile() is not working](https://stackoverflow.com/questions/41697980/laravel-request-hasfile-is-not-working) – steven7mwesigwa May 14 '22 at 06:20

0 Answers0