Laravel Call to a member function getClientOrigina

2019-05-22 22:34发布

问题:

Hello im getting the following error while upload an image using laravel: "Call to a member function getClientOriginalExtension() on null".

Here is my controller:

$imageName = rand(11111, 99999) . '.' . $request->file('image')->getClientOriginalExtension();
            $destinationPath = 'events';
            $fileName = rand(11111, 99999) . '.' . $extension;
            $upload_success = $image->move($destinationPath, $imageName);

Here is my view:

{!! Form::file('image', null, ['class' => 'form-control']) !!}

How do i save the $imageName to the pic field in my database. I tried this but it doesn't work. The field remains empty in the table.

$task=$request->user()->tasks()->create([
            'name' => $request->name,
            'description' => $request->description,
            'location' => $request->location,
            'pic' => $imageName,
        ]);

回答1:

in your form:open u need the 'files' => true like below

Form::open('your_path', array('files'=> true))

or

<form action="yout path" method="post" enctype="multipart/form-data">


回答2:

This means that no file input has been seen for that file. So, check that you input something in each file input