Alankar
Alankar

Reputation: 483

Laravel insert record in 2 tables from single model

I am using Lumen(Laravel), structure of 2 tables is given below

Language:
id,
lanugae_name,

Language_country
id,
language_id,
country_id,

I have created a model for lanugage table. But i want when i am inserting record in Language table on same time how i can insert its relationship with country in language_country ?

Upvotes: 2

Views: 71

Answers (2)

Piazzi
Piazzi

Reputation: 2636

You can use the attach() method

In your case:

$language->countries()->attach($countryId);

Upvotes: 2

James Clark
James Clark

Reputation: 1325

Assuming you have defined the relationship between Language and Country on the language model, you can use the attach() method to do this.

$language->countries()->attach($country->id);

Information about defining the relationship and more on the attach() method is at: https://laravel.com/docs/master/eloquent-relationships#updating-many-to-many-relationships

Upvotes: 5

Related Questions