Search code examples
laraveleloquenteloquent-relationship

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


Solution

  • Use like this

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