0

I am new to laravel 5.3. I need to upload the image to the database table spare. Here is my controller. The error i am getting is also attached.Please help thanks in advance.

 public function store(Request $request)
{
    try{
        $Spare=new Spares;
        $Spare->model_id=$request->get('modelId');
        $Spare->brand_id=$request->get('brandId');
        $Spare->partNumber=$request->get('partNumber');
        $Spare->warranty=$request->get('warranty');
        $Spare->retailer_id=$request->get('retailerId');
        $Spare->quantity=$request->get('quantity');
        $Spare->price=$request->get('price');
        $Spare->description=$request->get('description');
        $file = $request->file('image');

        // Get the contents of the file
        $contents = $file->openFile()->fread($file->getSize());

        $Spare->image=$contents;

        $Spare->save();
        \Session::flash('flash_message','Added new Spare.'); //<--FLASH MESSAGE

        return redirect()->back();
    }
    catch (\Illuminate\Database\QueryException $ex){
        dd($ex->getMessage());
    }
}

I am getting this error.

enter image description here

hEShaN
  • 551
  • 1
  • 9
  • 27
  • adding to the above answers always validate that you actually have a file, so in this case when you dont have you have a error cause $file is null – Amir Bar Oct 31 '16 at 17:14

2 Answers2

1

Since you haven't provided the HTML for this form I'll take a guess that your <form> tag is missing enctype="multipart/form-data". This attribute is required for uploading files.

deefour
  • 34,974
  • 7
  • 97
  • 90
1

You need to add the attribute enctype="multipart/form-data"for any kind of file uploads. Even if you are not using laravel you need to put the attribute and value as multipart/form-data.

You can read more on this here

Community
  • 1
  • 1
Shobi
  • 10,374
  • 6
  • 46
  • 82