Laravel更新或创建关系

I have an Item model, that has ItemTranslations, I'm receive a Request with the Item I'm updating and the translations for that Item.

Now some of those translations will already be in the database (they have their own id), and some of them will be new. Is there a clean way to go about this?

In pseudo: Update the Item with this request, for each Translations you find in this request, update the translation relation if it exists, else create it for this Item.

This is the layout of my Item.

class Item extends Model
{
    protected $fillable = ['menu_id', 'parent_id', 'title', 'order', 'resource_link', 'html_class', 'is_blank'];

    public function translations()
    {
        return $this->hasMany(MenuItemTranslation::class, 'menu_item_id');
    }
}

This is the ItemTranslation

class ItemTranslation extends Model
{
    protected $table = 'item_translations';
    protected $fillable = ['menu_item_id', 'locale', 'name', 'order', 'description', 'link', 'is_online'];
}

You could use the updateOrCreate method on the translations() relationship. For example, $item->translations()->updateOrCreate(['name' => $translation['name'], ], [ < the rest of the translation's data> ]); Note, that the first argument to the updateOrCreate method should be the data you want to look up the translation by, i.e. data that would stay the same. The second argument should be the data that can be updated.