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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay