Tuesday 18 June 2024

Deleting data using the API Resource to a Laravel 11 Model

Pre-requisites

We'll assume 3 things:

  1. That the model was created using the approach in this blog post.
  2. That the route was created using the approach in this blog post.
  3. That a migration file has been set using this blog post.
  4. That an Eloquent API Resource has been set up using this blog post.


Now open the YourController.php.

Develop the function destroy() like this:

public function destroy(YourModel $yourmodel)

{

$yourmodel->delete();

return response()->noContent();

}

To test this in Postman, you'll need to send a DELETE request with the URL of http://localhost/api/yourcontroller/1

In the Headers tab add the key Accept and the value of application/json

In the body select none instead of raw.

Try using an empty object to see if the API returns the validation message.

That should work.

No comments:

Post a Comment