Laravel Error -Response: :setStatusCode (): Argument #1 ($code) must be of type int, string given

Created At: 2023-10-09 21:54:32 Updated At: 2023-10-09 22:12:53

When you build Laravel api, you may encounter below error


"Symfony\ \Component\\HttpFoundation\\(Response: :setStatusCode

(): Argument #1 ($code) must be of type int, string given, called


There could be many reasons why you encounter this. But from the first glance, we don't know what's this error about. To know this error, we need to wrap our api around the below try catch clause

        }catch (\Throwable $th) {
            return response()->json([
                'status' => false,
                'message' => $th->getMessage()
            ], 500);

The magic happens inside catch clause. You need to use Throwable class and return $th->getMessage(), to know the exact error.

But to use you need to have special settings in Exception.php class in Laravel. Check out from the link. Once you have the correct setup, you may run your api test on postman. And you would be able to catch the exact error. In our case it was the database error. I forgot to send value for database field that has non-nullable property. 

So I have a non-nullable field which needs value during data insert.

For Laravel non-nullable field, you must assign value if there's no default value assigned for them. So I assigned value during data insert and value got resolved.


Add Reviews