簡體   English   中英

updateOrCreate為多態關系?

[英]updateOrCreate for polymorphic relation?

有沒有辦法使用updateOrCreate()或類似方法處理多態關系?

使用updateOrCreate方法的問題我不知道多態表中的id

目前這樣做:

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'),
        ]);
    }

}

像這樣的Cat模型的標記方法:

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

您需要傳遞第二個參數來updateOrCreate 嘗試將數組分成兩部分,如下所示:

$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