简体   繁体   English

updateOrCreate为多态关系?

[英]updateOrCreate for polymorphic relation?

Is there a way to use updateOrCreate() or similar method for polymorphic relationship? 有没有办法使用updateOrCreate()或类似方法处理多态关系?

Problem with using updateOrCreate method I wouldn't know the id in the polymorphic table. 使用updateOrCreate方法的问题我不知道多态表中的id

Currently doing like this: 目前这样做:

if ($request->has('meta.description')) {
    $description = $cat->tags()->where('key', 'description')->first();

    if (is_null($description)) {
        $cat->tags()->create([
            'client_id' => client()->id,
            'key'       => 'description',
            'value'     => $request->input('meta.description'),
        ]);
    } else {
        $description->update([
            'value' => $request->input('meta.description'),
        ]);
    }

}

Tag method like like this Cat model: 像这样的Cat模型的标记方法:

public function tags()
{
    return $this->morphMany('App\Tag', 'taggable', 'table_name', 'table_id');
}

You need to pass in a second parameter to updateOrCreate . 您需要传递第二个参数来updateOrCreate Try splitting the array up into two like so: 尝试将数组分成两部分,如下所示:

$cat->tags()->updateOrCreate(
    [
        'client_id' => client()->id,
        'key'       => 'description'
    ],
    [
        'value'     => $request->input('meta.description')
    ]
);

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

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