Loading episodes…
0:00 0:00

Eloquent Fill and Insert Method in Laravel 12.6

00:00
BACK TO HOME

Eloquent Fill and Insert Method in Laravel 12.6

10xTeam April 16, 2025 6 min read

The release of Laravel 12.6.0 brings several powerful additions to the framework’s toolkit, with the Eloquent fillAndInsert() method being a standout feature for developers working with database operations. This new method elegantly solves the challenge of merging model attributes before database insertion, facilitating more flexible and efficient data handling. The update also includes several other notable enhancements such as a URI path segments helper, password validation improvements, and changes to database seeding restrictions.

The New fillAndInsert Method: Purpose and Implementation

The Eloquent fillAndInsert() method represents a significant enhancement to Laravel’s ORM capabilities, introduced in version 12.6.0. Contributed by Luke Kuzmish, this method bridges a functionality gap in Eloquent’s data insertion workflow by allowing developers to merge model attributes before performing database insertions1.

Functionality and Syntax

At its core, the fillAndInsert() method enables developers to “manually cast values to primitives, set timestamps, or set UUIDs” before insertion, offering greater control over how data is processed1. The method can be used statically on model classes, accepting an array of arrays containing the records to be inserted: This approach provides a streamlined way to handle multiple records with varying attributes while ensuring proper attribute preparation through Eloquent’s machinery1. The implementation details can be found in Pull Request #55038 in the Laravel framework repository1.

Advantages Over Traditional Methods

The fillAndInsert() method offers several advantages over existing approaches:

  1. Automatic attribute processing: Unlike the raw insert() method, fillAndInsert() processes model attributes through Eloquent’s pipelines, ensuring proper casting and formatting1.
  2. Bulk operation support: While maintaining Eloquent’s attribute processing capabilities, it supports bulk operations similar to insert(), addressing a limitation of the create() method2.
  3. Timestamp handling: The method automatically manages timestamp fields, eliminating the need for manual timestamp assignment in bulk operations21.

Comparing Eloquent Database Methods

To understand the significance of the new fillAndInsert() method, it’s important to compare it with existing Eloquent methods for database operations.

The Eloquent Method Ecosystem

Laravel provides several methods for database interactions, each with distinct characteristics and use cases:

  1. fill(): Assigns values to a model instance without creating a new record. It requires a new instance before use and doesn’t automatically save data to the database3.
  2. save(): Used when you’ve already assigned values to a model instance. It can be used for both creating new records and updating existing ones3.
  3. create(): A static function that creates a new record using an array parameter. It combines instantiating a model, filling it with attributes, and saving it in one operation3.
  4. insert(): A Query Builder method that performs a direct database insertion and returns true/false depending on success2.

Key Differences Between Methods

The differences between these methods impact when and how they should be used:

Return Values

  • create() returns the complete model instance with all attributes
  • insert() returns only a boolean indicating success or failure2

Eloquent Features

  • create() triggers Eloquent’s features including:
    • Auto-filling timestamps
    • Accessors/mutators
    • Observers
    • Events/listeners
    • Traits
  • insert() is essentially a raw database query without these Eloquent features2

Multiple Record Support

  • insert() can handle multiple records in a single operation
  • create() works with only one record at a time2

How fillAndInsert Bridges the Gap

The new fillAndInsert() method effectively combines benefits from both worlds:

  1. It processes data through Eloquent’s attribute handling system like create()
  2. It supports bulk operations like insert()
  3. It manages model events and observers while still providing the efficiency of batch operations1

Other Notable Features in Laravel 12.6.0

While the fillAndInsert() method is a highlight, Laravel 12.6.0 introduces several other valuable features:

URI Path Segments Helper

Chester Sykes contributed a new pathSegments() method to the Uri class that returns each part of the URI path as a collection of elements1: This helper simplifies working with URI path components, offering a more elegant approach than manual string manipulation1.

Password Validation Improvements

The release includes an appliedRules() method for the Password fluent rule builder, which returns an array of password validation rules and their usage statuses1. This enables developers to dynamically display password requirements in views based on the actual validation rules in use: This feature enhances the user experience by ensuring consistency between validation logic and displayed requirements1.

Database Seeding Controls

Benedikt Franke contributed the ability to make the SeedCommand prohibitable in certain environments. After this update, you can call: This provides greater control over which database commands can be executed in production environments1.

Model Pruning Improvements

Günther Debrauwer improved the model:prune command to continue running after an exception, rather than failing entirely. This practical enhancement means that if pruning fails for one model, the command will skip it and continue with other models, reporting exceptions at the end1.

Conclusion

The introduction of the Eloquent fillAndInsert() method in Laravel 12.6.0 represents a significant evolution in Laravel’s ORM capabilities, addressing a common need for combining Eloquent’s attribute processing with bulk insertion operations. This method, along with other new features like URI path segments helpers and password validation improvements, demonstrates Laravel’s ongoing commitment to developer experience and framework refinement. For developers working with large datasets or complex model relationships, the fillAndInsert() method offers a more elegant and efficient approach to batch record creation while preserving the benefits of Eloquent’s robust attribute handling system. As Laravel continues to evolve, features like these help maintain its position as a leading PHP framework for web application development.

  1. https://laravel-news.com/laravel-12-6-0  2 3 4 5 6 7 8 9 10 11 12 13

  2. https://laraveldaily.com/post/eloquent-create-query-builder-insert  2 3 4 5 6

  3. https://stackoverflow.com/questions/72057804/difference-between-save-fill-create-in-laravel-eloquent  2 3


Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?