DEV Community

Cover image for How to delete data from one to many relationship in Laravel?
Mahmoud Adel
Mahmoud Adel

Posted on • Originally published at laravelspa.site on

How to delete data from one to many relationship in Laravel?

Delete data using the user form.

  • First go to routes/web.php file and add this route:
Route::get('/users/posts/delete', function () {
    $user = User::with('posts')->find(1);
    $user->posts()->whereIn('id', [1, 2])->delete();
    return response()->json($user);
});
Enter fullscreen mode Exit fullscreen mode
  • We open the browser and go to the new URL http://127.0.0.1:8000/users/posts/delete to find that the post has been deleted successfully.
{
  "id": 1,
  "username": "John Doe Updated",
  "created_at": "2023-09-06T17:24:02.000000Z",
  "updated_at": "2023-09-06T18:49:54.000000Z",
  "posts": [
    {
      "id": 5,
      "title": "Post title 5",
      "body": "Post body 5",
      "user_id": 1,
      "created_at": "2023-09-06T17:29:49.000000Z",
      "updated_at": "2023-09-06T17:29:49.000000Z"
    },
    {
      "id": 6,
      "title": "Post title 6",
      "body": "Post body 6",
      "user_id": 1,
      "created_at": "2023-09-06T17:29:49.000000Z",
      "updated_at": "2023-09-06T17:29:49.000000Z"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Delete data using the publication form.

  • First go to routes/web.php file and add this path:
Route::get('/posts/user/delete', function () {
    $post = Post::with('user')->findOrFail(2);
    $post->delete();
});
Enter fullscreen mode Exit fullscreen mode
  • We open the browser and go to the new URL http://127.0.0.1:8000/posts/user/delete. We see that the post has been successfully deleted. Rcord has deleted

Conclusion

This article is a continuation of the entire series on Laravel Eloquent Relationships Relationships within Laravel. We have covered one-to-many relationship in a complete manner. We have not spared any information for you, and, God willing, we will learn in the following explanation about the relationship of many to many.

  • You can find the repo of this series on github here

AWS Security LIVE! Stream

Go beyond the firewall

Security starts with people. Discover solutions to real-world challenges from AWS and AWS Partners on AWS Security LIVE!

Learn More

Top comments (0)

ACI image

ACI.dev: Best Open-Source Composio Alternative (AI Agent Tooling)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Star our GitHub!

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay