sackrin / laravel-meta-fields

为Laravel模型提供可定制的元字段

dev-master 2017-11-30 02:35 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:36:49 UTC


README

Laravel meta fields试图将流行的WordPress插件高级自定义字段的概念引入Laravel,以便快速创建丰富的内容和表单字段管理。元字段模式附加到模型上,并提供了丰富的文本、下拉框、wysiwyg等字段,可以进行管理和搜索。该想法不是取代模型属性,而是通过简化的键值对元表来扩展模型,使其具有可定制的专注于内容的元值。

快速开始

模型


namespace Example\Person\Model;

use Sackrin\Meta\Model\HasMetaFields;
use Sackrin\Meta\Model\MetaModel;
use Sackrin\Meta\Field\Blueprint;
use Sackrin\Meta\Field\Type\Group;
use Sackrin\Meta\Field\Type\Repeater;
use Sackrin\Meta\Field\Type\Text;


class Person extends Model implements MetaModel {

    use HasMetaFields;

    protected $table = 'people';

    /**
     * Indicates which model is responsible for this model's meta fields
     */
    public static $metaModel = PersonMeta::class;

    /**
     * Provide the meta fields blueprint
     * This object should contain all of the meta fields for this model
     */
    public static function fieldMetaBlueprint() {
        // Return the build field schema object
        return (new Blueprint())
            ->addBlueprint((new Text('company')))
            ->addBlueprint((new Group('details'))
                ->addBlueprint((new Text('first_name')))
                ->addBlueprint((new Text('surname')))
                ->addBlueprint((new Repeater('phones'))
                    ->addBlueprint((new Text('phone_number')))
                    ->addBlueprint((new Text('phone_area')))
                )
                ->addBlueprint((new Repeater('emails'))
                    ->addBlueprint((new Text('emailaddress')))
                )
            );
    }

}

    // Add to a migration to create the model table
    Schema::create('people', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
    

支持元模型。每个模型都应该有自己的元模型


namespace Example\Person\Model;

use Illuminate\Database\Eloquent\Model;

class PersonMeta extends Model {

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['machine', 'reference', 'path', 'position', 'type', 'value'];

    /**
     * Get the owner of the meta field
     */
    public function person()
    {
        return $this->belongsTo('Example\Person\Model\Person');
    }

}

    // Add to a migration to create the model meta table
    Schema::create('people_meta', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('people_id')->unsigned();
        $table->foreign('people_id')->references('id')->on('tutelage_activities');
        $table->text('machine');
        $table->text('reference');
        $table->text('path');
        $table->integer('position');
        $table->string('type');
        $table->longText('value')->nullable();
        $table->timestamps();
    });
    

设置字段

// Create or retrieve a person record then...
$person->setField('company', 'Acme Co.');
$person->save();

获取字段

// Create or retrieve a person record then...
$person->getField('company');