Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Call to a member function isValid() on null

Tags:

In my project users can upload stuff and attach an image. I have a controller where I do a isValid check on the file input. Everything works great as long as you select a file. When I don't select a file I get this error

Call to a member function isValid() on null

On this line of code:

if (Input::file('image')->isValid())

The error is obvious since I don't select an image but I actually have no idea how I can fix this.

If anyone can help I'd be very happy :) Thanks in advance!

like image 412
Cruzito Avatar asked May 28 '16 16:05

Cruzito


2 Answers

add enctype="multipart/form-data" to your form

like image 110
Ekene Oguikpu Avatar answered Sep 28 '22 04:09

Ekene Oguikpu


First you need to check if your request is containing that file / input or not.

Determining If A File Was Uploaded

if (Request::hasFile('image'))
{
    /*Determining If An Uploaded File Is Valid*/
    if (Request::file('image')->isValid())
    {
        //
    }
}

Just for your info also make sure you are getting file while posting. Add attribute enctype="multipart/form-data" in your HTML Form Element.

You can use any of Input and Request facade. Input class is also referencing to \Illuminate\Http\Request in laravel

like image 24
Pratik Soni Avatar answered Sep 28 '22 02:09

Pratik Soni