Error uploading images in laravel to database


I'm trying to upload an image to the database from a form

This is my view

{{ Form::open(array('url' => '/saveAbsence'), ['id' => 'absenceForm'], ['enctype'=>'multipart/form-data']) }}

This is my driver

public function storage(imageRequest $request){
       $image = $request->file('cover_image');
       $file = $image->store('img');

       $request->merge(['image'=> $file]);
       $cover = product::create($request->all());

But I get this error

"message": "Call to a member function store() on null"
asked by gmrYaeL 13.11.2018 в 14:27

1 answer


The error is that the $ cover variable gets an array with several files that may have been uploaded, so you must change

$cover = product::create($request->all());


$cover = product::create($request->first());

which does return a single object, and if you want to traverse the entire array, you must cycle from 0 to the length of the array.


answered by 14.11.2018 в 19:23