Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel response function only return 200?

For example:

response()->json(['error' => 'invalid', 401]);
response()->json(['success' => 'success', 200]);

These two ways are all response 200.If I want to return 200 when success,but 401 when error happened.How can I achieve this.

like image 600
ChenLee Avatar asked Jan 17 '17 09:01

ChenLee


People also ask

How can I return 200 in laravel?

For example: response()->json(['error' => 'invalid', 401]); response()->json(['success' => 'success', 200]); These two ways are all response 200.

How do I return a response in laravel?

Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response.


1 Answers

The 401 or 200 should be the second argument of the json function like this

response()->json(['error' => 'invalid'], 401);
response()->json(['success' => 'success'], 200);
like image 92
oseintow Avatar answered Sep 17 '22 15:09

oseintow