Laravel 在单个模型的 2 个表中插入记录
Laravel insert record in 2 tables from single model
我正在使用 Lumen(Laravel),下面给出了 2 tables 的结构
Language:
id,
lanugae_name,
Language_country
id,
language_id,
country_id,
我已经创建了一个语言模型 table。但是我想当我在语言 table 中同时插入记录时,我如何在 language_country 中插入它与国家/地区的关系?
假设你已经在语言模型上定义了Language和Country的关系,你可以使用attach()
方法来实现。
$language->countries()->attach($country->id);
有关定义关系的信息以及有关 attach() 方法的更多信息位于:https://laravel.com/docs/master/eloquent-relationships#updating-many-to-many-relationships
你可以使用attach()
方法
你的情况:
$language->countries()->attach($countryId);
我正在使用 Lumen(Laravel),下面给出了 2 tables 的结构
Language:
id,
lanugae_name,
Language_country
id,
language_id,
country_id,
我已经创建了一个语言模型 table。但是我想当我在语言 table 中同时插入记录时,我如何在 language_country 中插入它与国家/地区的关系?
假设你已经在语言模型上定义了Language和Country的关系,你可以使用attach()
方法来实现。
$language->countries()->attach($country->id);
有关定义关系的信息以及有关 attach() 方法的更多信息位于:https://laravel.com/docs/master/eloquent-relationships#updating-many-to-many-relationships
你可以使用attach()
方法
你的情况:
$language->countries()->attach($countryId);