tobymaxham/laravel-properties

该包可以通过属性为您的 Eloquent 模型添加一些功能。

v1.1 2023-06-18 15:21 UTC

This package is auto-updated.

Last update: 2024-09-18 18:01:10 UTC


README

Latest Version on Packagist Total Downloads

此包可以通过属性为您的 Eloquent 模型添加一些功能。

安装

您可以通过 composer 安装此包。

composer require tobymaxham/laravel-properties

用法

您的 Eloquent 模型应该使用 TobyMaxham\LaravelProperties\Traits\UseProperties 特性。

特性包含一些方法,帮助您处理数据库表列中的 JSON-Date。

您的模型迁移应该包含一个名为 properties 的字段来保存 JSON 数据。

以下是如何实现特性的示例

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TobyMaxham\LaravelProperties\Traits\UseProperties;

class YourEloquentModel extends Model
{
    use UseProperties;
}

及其迁移

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('your_eloquent_models', function (Blueprint $table) {
            $table->id();
            
            $table->json('properties');
            
            // ...
            
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('your_eloquent_models');
    }
};

用于在模型中存储值

$model = new EloquentModel();
$model->setProperty('key', 'value');
$model->save();

您也可以使用 Laravel 的 "点" 表示法来设置值

$model->setProperty('foo.bar', 'value');

从模型中获取值

$model->getProperty('foo.bar'); // 'value'

您也可以传递默认值

$model->getProperty('foo.baz', 'another Value'); // 'another Value'

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

安全漏洞

如果您发现有关安全的错误,请发送邮件至 git@maxham.de 而不是使用问题跟踪器。

支持我

ko-fi

致谢

许可

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