简体   繁体   English

更新laravel中的一对多关系

[英]update a one to many relationship in laravel

Suppose we have an Attribute model that can have some Option s. 假设我们有一个可以包含一些OptionAttribute模型。

On creating an attribute, we can specify some options for that. 创建属性时,我们可以为此指定一些选项。 in fact there is a one to many relation between them. 实际上,它们之间存在一对多的关系。

Attribute model is : Attribute模型是:

class Attribute extends Model
{
    use \Dimsav\Translatable\Translatable;

    protected $primaryKey = 'attribute_id';

    public $translatedAttributes = ['title'];
    protected $fillable = ['name', 'creator', 'type'];

    public function options()
    {
        return $this->hasMany(AttributeOption::class, 'attribute_id', 'attribute_id');
    }
}

And Option model is like this : Option模型是这样的:

class AttributeOption extends Model
{
    use \Dimsav\Translatable\Translatable;

    protected $fillable = ['attribute_id'];
    public $translatedAttributes = ['title'];
    public $timestamps = FALSE;

    protected $primaryKey = 'attribute_option_id';

    public function attribute()
    {
        return $this->belongsTo(Attribute::class, 'attribute_id', 'attribute_id');
    }

}

For save an attribute with it's options I wrote this : 为了保存带有选项的属性,我这样写:

\DB::transaction(function () use ($attributeGroup, $request) {

        $newAttribute = $attributeGroup->attributes()->create($request->all());

        if ($request->has('type') && $request->get('type') == 'select') {
            if ($request->has('options') and count($request->get('options')) > 0) {
                $options = $request->get('options');

                foreach ($options as $opt) {
                    $newAttribute->options()->create($opt);
                }
            }
        }

        return $this->item($newAttribute, new AttributeTransformer());
    });

But now consider when a user want to update an attribute that in this case , may want to delete some options and add new ones. 但是现在考虑在这种情况下用户想要更新属性时,可能想要删除一些选项并添加新选项。

And in this case I do not know how handle options. 在这种情况下,我不知道如何处理选项。 beacause I do not know how to recognize options that are removed that I can remove them from DB. 因为我不知道如何识别已删除的选项,因此可以将它们从数据库中删除。 and how can I update properties of options that does not touched and just fields like title , desc , ... are changed. 以及如何更新未触及的选项的属性,而只是更改了titledesc ,...之类的字段。

If I am understanding correctly, your issue relies on how to enable/disable options based on your current structure? 如果我理解正确,那么您的问题取决于如何根据当前结构启用/禁用选项? You might need another value in the table that represents active and inactive in that case, you could also use soft delete but that might just be a bit of an overkill if you are planning on activating and deactivating these options regularly. 在这种情况下,您可能需要表中的另一个值来表示活动状态和非活动状态,您也可以使用软删除,但是如果您打算定期激活和停用这些选项,那可能只是一个小问题。 On the last one, if you are simply changing values in the options, you would need to find a means of determining the option being modified by the user. 在最后一个选项中,如果仅更改选项中的值,则需要找到一种确定用户正在修改的选项的方法。 Be it by passing the id of the option from view to know what option exactly he is modifying, or some other means, at that point, if you can determine the option, you simply: 通过从视图传递选项的ID来知道他到底正在修改哪个选项,或者通过某种其他方式(如果可以确定该选项),只需:

$option->{value-to-be-changed} = {value};
$option->save();

$newAttribute->options()->sync([array of id's]) , but this works on Many to Many . $newAttribute->options()->sync([array of id's]) ,但这对多对多有效。 What sync does is that it deletes the relation that is not included in the array of id's. sync作用是删除ID数组中未包含的关系。

Reference question that might help you: 可帮助您的参考问题:

Synchronizing a one-to-many relationship in Laravel 在Laravel中同步一对多关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM