PHPackages                             devio/laravel-model-cast - 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. devio/laravel-model-cast

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

devio/laravel-model-cast
========================

1.0.1(2y ago)6291MITPHPPHP ^8.0

Since Jun 1Pushed 2y ago2 watchersCompare

[ Source](https://github.com/IsraelOrtuno/laravel-model-cast)[ Packagist](https://packagist.org/packages/devio/laravel-model-cast)[ Docs](https://github.com/IsraelOrtuno/laravel-model-cast)[ RSS](/packages/devio-laravel-model-cast/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (5)Used By (0)

Cast Eloquent Model Instances
=============================

[](#cast-eloquent-model-instances)

This package allows you to cast entire Eloquent models. Just like Eloquent's built-in feature for casting attributes but for Model instances.

Cast a base Eloquent model into class model which will share all its attributes but may have a different configuration or relationships. The casting occurs automatically when fetching from database. Here is a very basic example of how this may work:

```
class Car extends Model
{
  use CastsModel;
  // ...
}

class TeslaCar extends Car
{
}

$instance = Car::find(1);

$instance::class; // -> TeslaCar
```

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

[](#installation)

```
composer require devio/laravel-model-cast
```

Usage
-----

[](#usage)

You need at least 2 classes for this to work, a *generic* class which will be the base Model class with all common attributes and the specific casted class which will be resolved using those attributes.

Let's use cars in our example. `Car` will be our generic model and `TeslaCar` and `FordCar` the more specific classes the cars will be resolved to.

The base model should just use the `CastsModel` trait and implement the `getCastedModelClass` method which will be responsible of resolving which class should be this model be casted to based on its attributes data.

```
class Car extends Model
{
  use CastsModel;

  public function getCastedModelClass(array $attributes): string|null
  {
    // Add your custom resolution logic here based on the model attributes
    if ($attributes['brand'] == 'tesla') return TeslaCar::class;
    if ($attributes['brand'] == 'ford') return FordCar::class;

    return null;
  }
}

class TeslaCar extends Car {}
class FordCar extends Car {}
```

That's all to do. The underlying logic will be casting our `Car` instances to `TeslaCar` or `FordCar` when retrieving models:

```
$instance = Car::find(1);
$instance::class; // -> TeslaCar
```

It will also work right when creating a model using the `create` method:

```
$instance = Car::create(['brand' => 'tesla']);
$instance::class; // -> TeslaCar
```

Eloquent collections will be casted too:

```
$collection = Car::all();
$collection[0]::class; // -> TeslaCar
$collection[1]::class; // -> FordCar
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

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

Every ~20 days

Total

4

Last Release

1014d ago

Major Versions

0.1.1 → 1.0.02023-08-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/e40220fc9be76f3a9578c47d54172e37a492e48515dea55ab4197f6509d402a3?d=identicon)[IsraelOrtuno](/maintainers/IsraelOrtuno)

---

Top Contributors

[![IsraelOrtuno](https://avatars.githubusercontent.com/u/1769417?v=4)](https://github.com/IsraelOrtuno "IsraelOrtuno (17 commits)")

---

Tags

laraveleloquentcast

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/devio-laravel-model-cast/health.svg)

```
[![Health](https://phpackages.com/badges/devio-laravel-model-cast/health.svg)](https://phpackages.com/packages/devio-laravel-model-cast)
```

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[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)

PHPackages © 2026

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