pratikkuikel/wasabi

Laravel Json列键到模型属性

2.1 2024-10-03 07:19 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包将使用eloquent从数据库检索数据时将一级JSON列键转换为模型的属性。它仅在检索时工作。

安装

您可以通过composer安装此包

composer require pratikkuikel/wasabi

使用方法

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Pratikkuikel\Wasabi\Traits\Wasabi;

class State extends Model
{
    use HasFactory;
    use Wasabi;

    protected $guarded = [];

    protected $casts = [
        'data' => 'array'
    ];

    // OR
    // protected $casts = [
    //     'custom_field' => 'array'
    // ];

    // you don't need to define the constructor to use the default `data` field
    public function __construct(array $attributes = [])
    {
    // set wasabi's status false based on some condtions
        if($condition)
        {
         static::setWasabiStatus(true);
        }

    // Override the default dataField with any other preferred name
         static::setDataField('custom_field');

    // construct parent after setting data and status
         parent::__construct($attributes);
    }
}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关报告安全漏洞的详细信息,请参阅我们的安全策略

鸣谢

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅许可文件