drhtoo/laravel-metafield 问题修复 & 功能扩展

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

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

drhtoo/laravel-metafield

Composer 安装命令:

composer require drhtoo/laravel-metafield

包简介

Laravel MetaField

README 文档

README

The Laravel Metafield package allows you to add custom metafields to your Laravel Eloquent models. This is useful when you need to store additional data about a model beyond its basic attributes.

Installation

To install the package, you can simply run the following Composer command:

composer require drhtoo/laravel-metafield

Publishing Config and Migration (Optional)

After you've installed the package, you can optinally publish the package's config and migrations to your Laravel application. You can do this by running the following command:

php artisan vendor:publish --provider="Drhtoo\MetaField\LaravelMetaFieldServiceProvider"

Once the migrations have been published, you can run them by using the following command:

php artisan migrate

Usage

To start using the Laravel Metafield package, you'll need to add the HasMeta trait to any Eloquent models that you want to have metafields. Here's an example:

// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;
}

Now, you can use related meta fields by using relationship metas

$product = Product::find(1);

$product->metas->price = 100;

echo $product->metas->price; // 100

MetaFields Property

To use meta fields as a model attribute, you have to add $metaFields property to your model. It is a protected property of array type which is the default values keyed by the field/key.

// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => 'white',
    ];
}

$product = Product::find(1);

$product->price = 100;

echo $product->price; // 100

Attribute Casting

You can also cast the metafield as in attributes of Eloquent/Models.

// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => null,
        'sale_start' => null,
        'sale_end' => null,
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'sale_price' => 'decimal:2',
        'color' => 'array',
        'sale_start' => 'datetime',
        'sale_end' => 'datetime',
    ];
}

$product = Product::find(1);

$product->price = 100;

echo $product->price; // 100

Working with Livewire Component

This package is well compatible with Livewire component and directly bind to wire:model attribute. To be able to livewire component find the metafield attributes, you need to append the fields to model attributes.

// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta;

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'color' => null,
        'sale_start' => null,
        'sale_end' => null,
    ];

    protected $appends = [
        'price', 'is_sale', 'sale_price', 'color', 'sale_start', 'sale_end'
    ];
}

// Livewire component blade view
<input type="number" wire:model="product.price" />
@error('product.price')
<span class="error">{{ $message }}</span>
@enderror 

Working with Spatie's Laravel-translatable Package

Laravel Meta Field works well with spatie/laravel-translatable package and you just simply add array item of meta field to $translatable property of your model and use setAttribute method of HasMeta trait insteadof Translatable.

// Product.php
use Drhtoo\MetaField\Models\Concerns\HasMeta;

class Product extends Model
{
    use HasMeta {
        HasMeta::setAttribute insteadof HasTranslations;
    }

    protected $attributes = [
        'title' => null,
        'description' => null,
    ];

    protected $metaFields = [
        'price' => null,
        'is_sale' => false,
        'sale_price' => null,
        'sale_description' => null,
    ];

    public $translatable = [
        'title',
        'description',
        'sale_description'
    ];
}


$product = Product::find(1);

$product->sale_description = 'This is sale product'; // set value to current locale

$product->setTranslation('sale_description', 'es', 'Este es un producto de venta'); 

That's all. Have fun.

drhtoo/laravel-metafield 适用场景与选型建议

drhtoo/laravel-metafield 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 drhtoo/laravel-metafield 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drhtoo/laravel-metafield 我们能提供哪些服务?
定制开发 / 二次开发

基于 drhtoo/laravel-metafield 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-07