Logo

dev-resources.site

for different kinds of informations.

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

Published at
9/18/2023
Categories
laravel10
eloquent
relationships
howto
Author
devmahmoudadel
Author
14 person written this
devmahmoudadel
open
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
relationships Article's
30 articles in total
Favicon
Easier Relationship Mapping in the Backstage Catalog
Favicon
Understanding Self-Relationships in Laravel Models: A Simple Guide
Favicon
Kamagra 50 mg: Revitalize Your Love Life
Favicon
Turk Love Unveiled: A Journey to Lasting Joy in Relationships
Favicon
I want a divorce
Favicon
How to delete data from one to many relationship in Laravel?
Favicon
How to update a one-to-many relationship in Laravel?
Favicon
How can you retrieve data from a one-to-many relationship in Laravel?
Favicon
How to insert data in one to many relationship in database?
Favicon
How to create a One-To-Many relationship in Laravel?
Favicon
How to delete data from one to one relationship in Laravel?
Favicon
How to update a one-to-one relationship in Laravel?
Favicon
How can you retrieve data from a one-to-one relationship in Laravel?
Favicon
How to insert data in one to one relationship in database?
Favicon
How to create a One-To-One relationship in Laravel?
Favicon
What types of relationships are there in Laravel?
Favicon
HasOne Through Relationship
Favicon
How to run Monica personal CRM on Dokku
Favicon
Building Deep Relationships
Favicon
Why do I like to learn?
Favicon
The story of my year
Favicon
The lost message
Favicon
The TRUTH about sexy; hot questions with real answers! Podcast
Favicon
My memory of the time in secondary school
Favicon
What money cannot buy
Favicon
A love that not meant for me
Favicon
Avoid Ambiguous Column Eloquent Query Exception in Laravel
Favicon
Self-Referential m:n Relationships
Favicon
Ways to declare unsigned columns in the Laravel migrations
Favicon
Schema Design: Basic Tables & Relationships

Featured ones: