devio/laravel-model-cast 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devio/laravel-model-cast

最新稳定版本:1.0.1

Composer 安装命令:

composer require devio/laravel-model-cast

包简介

README 文档

README

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

composer require devio/laravel-model-cast

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

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-01