mardi 6 février 2018

updateOrCreate for polymorphic relation?

Is there a way to use updateOrCreate() or similar method for polymorphic relationship?

Problem with using updateOrCreate method I wouldn't know the id in the polymorphic table.

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:

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire