6
votes

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,
        ]);
4
make sure you do a multipart post. It looks like the file sis not uploadedMaGnetas

4 Answers

19
votes

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

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

or

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

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

0
votes

Just simply add the following code inside your form's opening tag: enctype="multipart/form-data"

0
votes

First please check Camilo Rojas accepted solution in here and then check both of input name and file parameter, names must be same.

Controller:

$request->file('image')->getClientOriginalExtension();

View:

<input type="file" name="image"/>