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

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

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

drhtoo/laravel-metafield

最新稳定版本:v1.0.0

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.

统计信息

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

GitHub 信息

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

其他信息

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