PHPackages                             laravel-macros/eloquent-insert-as-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. laravel-macros/eloquent-insert-as-model

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

laravel-macros/eloquent-insert-as-model
=======================================

Add `insertAsModel` macro to Eloquent Builder. Unlike `insert`, `insertAsModel` method will ensure that all inserted values will go through models' casts and mutators.

1.0.0(5y ago)04MITPHPPHP ^8.0

Since Apr 25Pushed 5y ago1 watchersCompare

[ Source](https://github.com/laravel-macros/eloquent-insert-as-model)[ Packagist](https://packagist.org/packages/laravel-macros/eloquent-insert-as-model)[ Docs](https://github.com/laravel-macros/eloquent-insert-as-model)[ RSS](/packages/laravel-macros-eloquent-insert-as-model/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Laravel Macros: Eloquent | `insertAsModel`
==========================================

[](#laravel-macros-eloquent--insertasmodel)

This package will add `insertAsModel` macro to Laravel's Eloquent Builder class.

Unlike `insert`, `insertAsModel` method will ensure that all inserted values will go through models' casts and mutators then it will just pass it to the `insert` method.

[![Latest Version on Packagist](https://camo.githubusercontent.com/733b1c4aa122e88df63bfc29e487fadfb0bde7f59a49dc89df03339b6189da26/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726176656c2d6d6163726f732f656c6f7175656e742d696e736572742d61732d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravel-macros/eloquent-insert-as-model)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b7ed43baefed737c645112dbb310766b23026a3842da1727bced2498a3b6d57b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c61726176656c2d6d6163726f732f656c6f7175656e742d696e736572742d61732d6d6f64656c2f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/laravel-macros/eloquent-insert-as-model/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/8d7b9cdd096fb5a2783110cf7d1cf91e24256b8e40a34fee96697e0da312a230/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c61726176656c2d6d6163726f732f656c6f7175656e742d696e736572742d61732d6d6f64656c2f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/laravel-macros/eloquent-insert-as-model/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/3a2772d7b8292512b89d8cd7f51972b2866118a9340ddc2b34427ac78406a5fe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c2d6d6163726f732f656c6f7175656e742d696e736572742d61732d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravel-macros/eloquent-insert-as-model)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require laravel-macros/eloquent-insert-as-model
```

Usage
-----

[](#usage)

Assuming you have:

```
class User extends Model
{
     protected $casts = [
        'options' => 'json',
        'shelf'   => 'collection',
    ];
}
```

Normally if you will use the `Model::insert` method to insert a batch of entries, casts and mutators will be ignored and you will have to stringify the values yourself.

Using this package, you can insert values the same way you use `Model::create`, where all the proper casting and mutating logic will be applied.

```
User::insertAsModel([
    [
        'options' => ['sms' => true],
        'shelf'   => collect(['book1', 'book2', 'book3']),
    ],
    [
        'options' => ['sms' => false],
        'shelf'   => collect(['book1', 'book2']),
    ],
]);
```

This will prevent you from doing hacks like `'options' => json_encode(['sms' => true])`.

> Note: `insertAsModel` is not a multiple `create` calls. It will insert entries directly to the database (just like `insert`). For example it won't fire models events.

Testing
-------

[](#testing)

```
composer test

# Test Coverage with XDebug enabled
composer test-coverage
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Mohannad Najjar](https://github.com/MohannadNaj)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1843d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18e0b66d27f3490031bce184e2fff5343a98f895676871a5b43ff2d3774f9690?d=identicon)[Mohannad](/maintainers/Mohannad)

---

Top Contributors

[![MohannadNaj](https://avatars.githubusercontent.com/u/8743700?v=4)](https://github.com/MohannadNaj "MohannadNaj (6 commits)")

---

Tags

laraveleloquentmacro

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/laravel-macros-eloquent-insert-as-model/health.svg)

```
[![Health](https://phpackages.com/badges/laravel-macros-eloquent-insert-as-model/health.svg)](https://phpackages.com/packages/laravel-macros-eloquent-insert-as-model)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
