7 Easy Type of Save Methods in Laravel 10

In Laravel, there are several methods for saving data to the database. Here are the common methods used to save data:

  1. Using thesave() 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();
    
  2. Using thecreate() 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',
     ]);
    
  3. Using theupdateOrCreate() 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
     );
    
  4. Using thefirstOrCreate() 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
     );
    
  5. Using theupdate() method: This method updates one or more existing records in the database.

     $post = Post::where('id', 1)->update(['title' => 'Updated Title']);
    
  6. Using thefill() and save() methods together: You can use the fill() method to assign attributes and then save the model with the save() method.

     $post = new Post();
     $post->fill([
         'title' => 'New Post',
         'content' => 'Lorem ipsum dolor sit amet',
     ])->save();
    
  7. Using theinsert() 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

  1. 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);
    
  2. 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);
    
  3. 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);
    
  4. 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
    
  5. 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

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!