承接 raveren/eloquent-data-object 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

raveren/eloquent-data-object

Composer 安装命令:

composer require raveren/eloquent-data-object

包简介

Store strictly typed PHP objects in Eloquent JSON columns. Data class changes never invalidate stored data.

README 文档

README

Store strictly typed PHP objects in Eloquent JSON columns. Data class changes never invalidate stored data.

Installation

composer require raveren/eloquent-data-object

What this does

Your model column becomes a strictly typed object!

image

Any changes to your data classes in the future don't invalidate stored data!

How it works

Just extend EloquentDataObject (or see below if you can't):

<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Raveren\EloquentDataObject\EloquentDataObject;

class MyData extends EloquentDataObject #####   <---- Step 1/3: extend EloquentDataObject ######
{
    public string $name;
    /** @var MyData[] */
    public array $children;
}

class MyModel extends Model
{
    protected function casts(): array
    {
        return [
            'json_data' => MyData::class #####   <---- Step 2/3: add Eloquent cast       ######
        ];
    }
}

Schema::table('my_table', function (Blueprint $table) {
    $table->json('json_data')->nullable(); ##### <---- Step 3/3: create DB column        ######
});

🎉 Done!

Objects are stored as JSON inside the database.

In PHP $myModel->json_data is always an instance of MyData.

Thus, if you receive an associative-array representation, you must convert it to MyData before saving:

$myModel->json_data = MyData::from($array); // helper method comes in the box

If your data class changes

  1. No data is ever lost, and you get no errors (see 5. below).
  2. If you rename or remove a class attribute, existing data will be preserved as a Dynamic property with the old name on the class instance (no data loss).
  3. To fix drifted names inside the database, define a renamedDataObjectColumns():
class MyData extends EloquentDataObject
{
    public string $newName;
    
    public static function renamedDataObjectColumns(): array // <----- This
    {
        return [
            'name'     => 'newName',
            'children' => null, // Set new name to null to drop the value from storage on saving.
        ];
    }
}
  1. It's enough to load each model once and save it to fix the data inside the DB permanently, so you don't have to keep the renamedDataObjectColumns() method if you do that.
  2. Attributes that appear in database JSON, but are missing in class definitions issue Log::error() with detailed information. You can provide your own error handler instead, example:
// app/Providers/AppServiceProvider.php
EloquentDataObject::setMissingAttributeHandler(
    static fn(string $targetClassname, string $currentClassname, string $missingKey) => report(
        new Exception(
            sprintf(
                'EloquentDataObject cast for %s has unrecognized property %s->%s. To solve, extend `renamedDataObjectColumns(): array` in your data class.',
                $targetClassname,
                $targetObject::class,
                $key,
            ),
        ),
    );
);

If you cannot extend a class :

  1. Add use EloquentDataObjectTrait;
  2. Add implements Castable
  3. Annotate with #[AllowDynamicProperties] (only if you renamed class attributes)
#[AllowDynamicProperties]
class MyData implements Castable
{
    use EloquentDataObjectTrait;
    // ...
}

Acknowledgments

Hierarchical object conversion is handled by netresearch/jsonmapper (sole project dependency).

Licence

Unlicence, do whatever you want with it.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2026-05-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固