How to insert data into two one to many relationship tables at the same time

I am working on website where user should enter the category of food along with other infos

enter image description here

There are two tables category and menu, I made one to many relationship: enter image description here

enter image description here

enter image description here

Now I am trying to store the informatin that I got from user into these tables, and I don't really know how to do that? Below I am trying to that. Could you please help me out with that? enter image description here

Upvotes: 0

Views: 151

Answers (2)

A.A Noman
A.A Noman

Reputation: 5270

Use like this

$menu = new Menu($menu);
$menu->category()->associate("Your_Category_ID");
$menu->save();

Upvotes: 1

void
void

Reputation: 913

If Single Insert a time:

$arr1 = [
'category' => $request->category,
]
$data = Category::create($arr);
if($data) {
$arr2 = [
'title' => $request->title,
'cost' => $request->cost,
'body' => $request->body,
'category_id' => $data->id
]
Menu::create($arra2);
}

And if its multiple insert then create two blank array and push the $arr1 and $arr2 respectively And use insert() to multiple insert

Upvotes: 0

Related Questions