pratikkuikel / wasabi
Laravel Json列键到模型属性
2.1
2024-10-03 07:19 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.16.5
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.4
- orchestra/testbench: ^9.5
- pestphp/pest: ^3.2
- pestphp/pest-plugin-arch: ^3.0.0
- pestphp/pest-plugin-laravel: ^3.0.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
This package is auto-updated.
Last update: 2024-10-03 07:22:13 UTC
README
此包将使用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)。有关更多信息,请参阅许可文件。