title: Inserting a has many relationship tables at the same time in laravel
body: <p>I am using PHP Laravel Framework.I have 1 to M relationship tables <code>order</code> and <code>products</code>.</p>
  
  <p><code>ORDER TABLE  - orderid &lt;--autoincrement primary key</code>  <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>- orderdate</code>  <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>- Name</code><br></p>
  
  <p><code>PRODUCTS TABLE  - productid &lt;--autoincrement primary key</code> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>- orderid &lt;--foreign key reference to order table</code>  <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>- productName</code><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>- price</code><br>
  My models are as below --> <br>
  <strong>Order Model :</strong><br></p>
  
  <pre><code>class Order extends Eloquent 
  {
  
      protected $table = 'order';
  
      public function products()
      {
          return $this-&gt;hasMany('Products','orderid');
      }
  
  }
  </code></pre>
  
  <p><strong>Products Model :</strong><br></p>
  
  <pre><code>class Products extends Eloquent 
  {
      protected $table = 'products';
  }
  </code></pre>
  
  <p>I have a form where I am taking the order date,name of the customer and the products details that can be more than one products.User can have one or many products in one order.<br>So now i have to insert details about both the tables in one go .<br>
  I read this doc <a href="http://laravel.com/docs/eloquent#inserting-related-models" rel="noreferrer">http://laravel.com/docs/eloquent#inserting-related-models</a> <br>they have given this below code for inserting related models --></p>
  
  <pre><code>$comments = array(
      new Comment(array('message' =&gt; 'A new comment.')),
      new Comment(array('message' =&gt; 'Another comment.')),
      new Comment(array('message' =&gt; 'The latest comment.'))
  );
  
  $post = Post::find(1);
  
  $post-&gt;comments()-&gt;saveMany($comments);
  </code></pre>
  
  <p>But here they know the id to find from the parent table but in my case I have to insert details in the Order table and then the details about the products in the Products table for the order which was inserted just before . <strong>Problem is how to find the newly inserted orderid</strong> ? I am using autoincrement for <code>orderid</code> field in the Order table.<br></p>