In Laravel, there are several methods for saving data to the database. Here are the common methods used to save data:
Using the
save()
method on an instance of the model: This method saves a new model instance or updates an existing one.$post = new Post(); $post->title = 'New Post'; $post->content = 'Lorem ipsum dolor sit amet'; $post->save();
Using the
create()
method: This method creates a new model instance and saves it to the database in a single step.$post = Post::create([ 'title' => 'New Post', 'content' => 'Lorem ipsum dolor sit amet', ]);
Using the
updateOrCreate()
method: This method updates an existing model or creates a new one if it doesn't exist.$post = Post::updateOrCreate( ['title' => 'Existing Post'], // Conditions to find existing model ['content' => 'Updated content'] // Data to update or create );
Using the
firstOrCreate()
method: This method retrieves the first model matching the given conditions or creates a new one if no match is found.$post = Post::firstOrCreate( ['title' => 'Existing Post'], // Conditions to find existing model ['content' => 'New content'] // Data to create if not found );
Using the
update()
method: This method updates one or more existing records in the database.$post = Post::where('id', 1)->update(['title' => 'Updated Title']);
Using the
fill()
andsave()
methods together: You can use thefill()
method to assign attributes and then save the model with thesave()
method.$post = new Post(); $post->fill([ 'title' => 'New Post', 'content' => 'Lorem ipsum dolor sit amet', ])->save();
Using the
insert()
method for bulk inserts: This method inserts multiple records into the database with a single query.$data = [ ['title' => 'Post 1', 'content' => 'Content 1'], ['title' => 'Post 2', 'content' => 'Content 2'], ]; Post::insert($data);
These are the main methods for saving data in Laravel. Choose the appropriate method based on your specific use case and requirements.
Save Relation data example Like Comment into Post
// Find the post by its ID
$post = Post::findOrFail($request->post_id);
// Create a new comment instance
$comment = new Comment();
$comment->content = $request->content;
// Associate the comment with the post
$post->comments()->save($comment);
more type of methods from relation data save
Attaching Related Models with Many-to-Many Relationships: If you have a many-to-many relationship between two models, you can use the
attach()
method to attach related models to each other.$post = Post::find($postId); $tagIds = [1, 2, 3]; // IDs of the tags to attach $post->tags()->attach($tagIds);
Detaching Related Models with Many-to-Many Relationships: You can use the
detach()
method to remove the association between related models in a many-to-many relationship.$post = Post::find($postId); $tagIds = [1, 2]; // IDs of the tags to detach $post->tags()->detach($tagIds);
Syncing Related Models with Many-to-Many Relationships: The
sync()
method is useful when you want to synchronize the related models in a many-to-many relationship. This method accepts an array of IDs and will attach, detach, and update the related models as necessary to match the given array.$post = Post::find($postId); $tagIds = [1, 3, 5]; // IDs of the tags to synchronize $post->tags()->sync($tagIds);
Updating Existing Related Models: If you have existing related models and you want to update them, you can use the
update()
method directly on the relationship.$post = Post::find($postId); $post->comments()->update(['approved' => true]); // Update all comments to be approved
Saving Related Models with One-to-One Relationships: For one-to-one relationships, you can save related models using the
save()
method on the relationship.$user = User::find($userId); $profile = new Profile(['bio' => 'Lorem ipsum']); $user->profile()->save($profile);
Read More code.yoblogger.com