mits87/eloquent-nested-attributes

嵌套属性允许您通过父记录保存属性。默认情况下,嵌套属性更新是关闭的,您可以使用$nested属性来启用它。当您启用嵌套属性时,模型上定义了一个属性写入器。

0.0.6 2017-11-23 08:40 UTC

This package is auto-updated.

Last update: 2024-09-03 10:49:19 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

嵌套属性允许您通过父记录保存属性。默认情况下,嵌套属性更新是关闭的,您可以使用$nested属性来启用它。当您启用嵌套属性时,模型上定义了一个属性写入器。

结构

如果以下任何一项适用于您的项目,则目录结构应遵循行业最佳实践,并命名为以下。

src/
tests/
vendor/

安装

通过Composer

$ composer require mits87/eloquent-nested-attributes

用法

namespace App;

use Eloquent\NestedAttributes\Model;

class Post extends Model
{
    protected $fillable = ['title'];
    
    protected $nested = ['option', 'comments'];

    public function option() {
        //it can be also morphOne
        return $this->hasOne('App\Option');
    }

    public function comments() {
        //it can be also morphMany
        return $this->hasMany('App\Comment');
    }
}

namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent\NestedAttributes\Traits\HasNestedAttributesTrait;

class Post extends Model
{
    use HasNestedAttributesTrait;

    ...
}

用法

\App\Post::create([
    'title' => 'Some text',

    'option' => [
        'info' => 'some info'
    ],

    'comments' => [
        [
            'text' => 'Comment 1'
        ], [
            'text' => 'Comment 2'
        ],
    ]
]);


\App\Post::findOrFail(1)->update([
    'title' => 'Better text',

    'option' => [
        'info' => 'better info'
    ],

    'comments' => [
        [
            'id' => 2,
            'text' => 'Comment 2'
        ],
    ]
]);

要删除嵌套行,应传递_destroy属性

\App\Post::findOrFail(1)->update([
    'title' => 'Better text',

    'option' => [
        'info' => 'better info'
    ],

    'comments' => [
        [
            'id' => 2,
            '_destroy' => true
        ],
    ]
]);

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCODE_OF_CONDUCT以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件mits87@gmail.com而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。