0

I've newly installed Laravel 8, and setup my api resources.

But when I try to create/update a record, it's redirect me with 302to the home page...

  • Here is my api.php:
    Route::apiResource('addresses', AddressController::class);
  • In my AddressController.php, my store method:
    public function store(CreateAddressRequest $request)
    {
        return response()->json(Address::create($request->validated()));
    }

Need help (to understand), please.

2 Answers2

1

Please set the header in postman - Accept: application/json.

0

When you create through store() method, you have CreateAddressRequest class which validates your input. When the validation fails it redirects to the previous page and it could redirects to the homepage if there is no previous page.

For API, this behavior is not desirable since you want to return error message (in JSON) instead of redirection.

Example of using validator to return the error. REST API in Laravel when validating the request

Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31