albinodrought/laravel-fillable-relations

该包已被弃用,不再维护。未建议替代包。

为 Eloquent 模型提供 HasFillableRelations 特性

v1.1.0 2018-04-26 23:07 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:53:01 UTC


README

Build Status

此库为 Eloquent 模型提供了一个混合特性,从而支持可填充关系。

这是一个更严格、版本化且具有偏见的https://github.com/troelskn/laravel-fillable-relations分支。

安装

composer require albinodrought/laravel-fillable-relations

使用

首先,在你的模型中

<?php
namespace MyApp\Models;

use Illuminate\Database\Eloquent\Model;
use LaravelFillableRelations\Eloquent\Concerns\HasFillableRelations;

class Foo extends Model
{
    use HasFillableRelations;
    protected $fillableRelations = ['bar'];

    function bar()
    {
        return $this->hasOne(Bar::class);
    }
}

class Bar extends Model
{
    use HasFillableRelations;
    protected $fillableRelations = ['foos'];

    function foos()
    {
        return $this->hasMany(Foo::class);
    }
}

现在你可以填充关系,如下所示

<?php
$foo = new Foo(
    [
        'cuux' => 42,
        'bar' => [
            'id' => 42
        ]
    ]
);

或者

<?php
$foo = new Foo(
    [
        'cuux' => 42,
        'bar' => [
            'name' => "Ye Olde Pubbe"
        ]
    ]
);

也可以

<?php
$bar = new Bar(
    [
        'name' => "Ye Olde Pubbe",
        'foos' => [
            [
                'cuux' => 42
            ],
            [
                'cuux' => 1337
            ]
        ]
    ]
);

测试

composer test