The HTTP status code "0" is not valid

Viewed 20020

The HTTP status code "0" is not valid.

public function filter()
{
$institute = (new Institute)->newQuery();
$institute->with(['locations','courses' =>  function ($query) use ($request){
            $query->with('trainers');
        }]);
}
$data = $institute->get();
if(count($data) > 0)
{
    return response()->json($data);
}
else
{
    return response()->json(404,'No Data found');
}
}

Actually, I want to display error 404 message if there is no data,

my problem is when I try to check whether data is there or not I'm getting an error called InvalidArgumentException.Can anyone help on this please.

4 Answers
return response()->json(404,'No Data found');

The first argument of json should be data, then goes a status code. In this case the status code gets 0 value. Simply do it as follows:

return response()->json('No Data found', 404);

This error is thrown when you try and use 0 as your response exception code. In my case my App/Exceptions/Handler.php file was modified and I returned 0 as my HTTP code after formatting which is not allowed. Just make sure you are returning a valid HTTP code from your Exception handler at all times.

Another case can cause the same problem when you have a difference between code and database schema.

In laravel, I had this issue when trying to do an insert on a table where I had the field type incorrect. In my specific case, I was trying to insert a varchar into an integer field in my DB Schema.

Changed the Schema to varchar and I was all set!

Related