Faruh
Faruh

Reputation: 9

How to return json with http code instead redirect Laravel?

I use Form Requests to incapsulate validation logic into files then to use it like:

   public function register(RegisterRequest $request) {}

When validation is false it redirects to the index page.

How to return json with http code instead redirection?

Upvotes: 0

Views: 212

Answers (2)

amin hd
amin hd

Reputation: 81

use following code :

return \response($data, $statusCode);

for example :

return \response(['name'=>'Amin'], 200);

Upvotes: 1

Saroj Shrestha
Saroj Shrestha

Reputation: 2875

From postman, you have to pass Accept as application/json on header tab

enter image description here

Upvotes: 1

Related Questions