dev-resources.site
for different kinds of informations.
How to update a 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
Update data using the user form.
1- Using push method
.
- First go to
routes/web.php
file and modify this route:
Route::get('/users/update', method () {
$user = User::with('posts')->find(1);
$post = $user->posts()->whereId(1)->first();
$post->title = 'Post title 1 updated';
$post->push();
return response()->json($user);
});
- We open the browser and go to the new URL
http://127.0.0.1:8000/users/update
to find that the post has been updated successfully.
{
"id": 1,
"username": "John Doe",
"created_at": "2023-09-06T17:24:02.000000Z",
"updated_at": "2023-09-06T17:25:58.000000Z",
"posts": [
{
"id": 1,
"title": "Post title 1 updated",
"body": "Post body 1",
"user_id": 1,
"created_at": "2023-09-06T17:28:58.000000Z",
"updated_at": "2023-09-06T18:37:30.000000Z"
},
{
"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"
}
]
}
2- Using update method
.
- First go to the file
routes/web.php
and modify this route.
Route::get('/users/update', method () {
$user = User::with('posts')->find(1);
$post = $user->posts()->whereId(1)->first();
$post->title = 'Post title 1';
$post->update();
return response()->json($user);
]);
- We open the browser and go to the new URL
http://127.0.0.1:8000/users/update
to find that the post has been updated successfully.
{
"id": 1,
"username": "John Doe",
"created_at": "2023-09-06T17:24:02.000000Z",
"updated_at": "2023-09-06T17:25:58.000000Z",
"posts": [
{
"id": 1,
"title": "Post title 1",
"body": "Post body 1",
"user_id": 1,
"created_at": "2023-09-06T17:28:58.000000Z",
"updated_at": "2023-09-06T18:41:35.000000Z"
},
{
"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"
}
]
}
Update data using the post form.
1- Using push method
.
- First go to
routes/web.php
file and add this route:
Route::get('/posts/update', method () {
$post = Post::with('user')->find(1);
$post->title = 'Post title 1 updated';
$post->user->username = 'John Doe Updated';
$post->push();
return response()->json($post);
});
- We open the browser and go to the new URL
http://127.0.0.1:8000/posts/update
to find that the post has been updated successfully.
{
"id": 1,
"title": "Post title 1 updated",
"body": "Post body 1",
"user_id": 1,
"created_at": "2023-09-06T17:28:58.000000Z",
"updated_at": "2023-09-06T18:50:30.000000Z",
"user": {
"id": 1,
"username": "John Doe Updated",
"created_at": "2023-09-06T17:24:02.000000Z",
"updated_at": "2023-09-06T18:49:54.000000Z"
}
}
2- Using update method
.
- First go to the file
routes/web.php
and modify this route.
Route::get('/posts/update', method () {
$post = Post::with('user')->find(1);
$post->user->username = 'John Doe';
$post->update([
'title' => 'Post title 1'
]);
return response()->json($post);
]);
- We open the browser and go to the new URL
http://127.0.0.1:8000/posts/update
to find that the post has been updated successfully.
{
"id": 1,
"title": "Post title 1",
"body": "Post body 1",
"user_id": 1,
"created_at": "2023-09-06T17:28:58.000000Z",
"updated_at": "2023-09-06T18:55:45.000000Z",
"user": {
"id": 1,
"username": "John Doe",
"created_at": "2023-09-06T17:24:02.000000Z",
"updated_at": "2023-09-06T18:49:54.000000Z"
}
}
- You can find the repo of this series on github here
relationships Article's
30 articles in total
Easier Relationship Mapping in the Backstage Catalog
read article
Understanding Self-Relationships in Laravel Models: A Simple Guide
read article
Kamagra 50 mg: Revitalize Your Love Life
read article
Turk Love Unveiled: A Journey to Lasting Joy in Relationships
read article
I want a divorce
read article
How to delete data from one to many relationship in Laravel?
read article
How to update a one-to-many relationship in Laravel?
currently reading
How can you retrieve data from a one-to-many relationship in Laravel?
read article
How to insert data in one to many relationship in database?
read article
How to create a One-To-Many relationship in Laravel?
read article
How to delete data from one to one relationship in Laravel?
read article
How to update a one-to-one relationship in Laravel?
read article
How can you retrieve data from a one-to-one relationship in Laravel?
read article
How to insert data in one to one relationship in database?
read article
How to create a One-To-One relationship in Laravel?
read article
What types of relationships are there in Laravel?
read article
HasOne Through Relationship
read article
How to run Monica personal CRM on Dokku
read article
Building Deep Relationships
read article
Why do I like to learn?
read article
The story of my year
read article
The lost message
read article
The TRUTH about sexy; hot questions with real answers! Podcast
read article
My memory of the time in secondary school
read article
What money cannot buy
read article
A love that not meant for me
read article
Avoid Ambiguous Column Eloquent Query Exception in Laravel
read article
Self-Referential m:n Relationships
read article
Ways to declare unsigned columns in the Laravel migrations
read article
Schema Design: Basic Tables & Relationships
read article
Featured ones: