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.
For example: response()->json(['error' => 'invalid', 401]); response()->json(['success' => 'success', 200]); These two ways are all response 200.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With