Ad

Call To A Member Function GetClientOriginalName() On Null When Upload Image Use File System Laravel

- 1 answer

I want to upload an image using Laravel storage file system in my admin data. However, there's an error when I attempt to upload an image.

Call to a member function getClientOriginalName() on null

Controller

public function store(Request $request)
{
    $admin              = $request->all();
    $fileName           = $request->file('foto')->getClientOriginalName();
    $destinationPath    = 'images/';
    $proses             = $request->file('foto')->move($destinationPath, $fileName);

    if($request->hasFile('foto'))
    {
        $obj = array (
            'foto'  => $fileName,
            'nama_admin' => $admin['nama_admin'],
            'email' => $admin['email'],
            'jabatan' => $admin['jabatan'],
            'password' => $admin['password'],
            'confirm_password' => $admin['confirm_password']
        );
        DB::table('admins')->insert($obj);
    }
    return redirect()->route('admin-index');
}

View

<div class="form-group">
    <label for="" class="col-md-4">Upload Foto</label>
    <div class="col-md-6">
        <input type="file" name="foto">
    </div>                
 </div>

Errorerror

Ad

Answer

You can check wheather you are getting file or not by var_dump($request->file('foto')->getClientOriginalName());

And make sure your form has enctype="multipart/form-data" set

<form enctype="multipart/form-data" method="post" action="{{ url('/store')}}">
<div class="form-group">
    <label for="" class="col-md-4">Upload Foto</label>
    <div class="col-md-6">
        <input type="file" name="foto">
    </div>                
</div>
</form>
Ad
source: stackoverflow.com
Ad