Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The HTTP status code "0" is not valid

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.

like image 272
Manjunath Avatar asked Dec 23 '22 14:12

Manjunath


1 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);
like image 165
Sergei Krivosheenko Avatar answered Jan 12 '23 17:01

Sergei Krivosheenko