hEShaN hEShaN - 1 month ago 22
PHP Question

Laravel 5.3 image upload issue

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

Answer

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.