title: How I can save data in two tables simultaneously in laravel?
body: <p>I have the following data mode
  <img src="https://i.stack.imgur.com/FIBsa.png" alt="enter image description here">l</p>
  
  <p><code>Detalle_Servicio</code> has many <code>Material_Usado</code> and <code>Material_Usado</code> belongs only <code>Detalle_Servicio</code></p>
  
  <p>When I save values ​​in Material_Usado should know the ID Detelle_Servido, but as I can not do that. I have the following code</p>
  
  <p><strong>Route</strong></p>
  
  <pre><code>Route::post('/upload', function(){
  
      $path = public_path().'/servicios';
      try{
          $upload_success1 = Input::file('foto1')-&gt;move($path,'1');
          $upload_success2 = Input::file('foto2')-&gt;move($path,'2');
          $upload_success3 = Input::file('foto3')-&gt;move($path,'3');
          $upload_success4 = Input::file('foto4')-&gt;move($path,'4');
      }catch(Exception $e) {
          $e-&gt;getMessage();
      }
      $input = Input::get('json');
      $json = json_decode($input);
  
      if($upload_success1 &amp;&amp; $upload_success2 &amp;&amp; $upload_success3 &amp;&amp; $upload_success4) {
          //DB::insert("INSERT INTO Detalle_Servicio (RutaFoto1, RutaFoto2, RutaFoto3, RutaFoto4, FechaTermino, Latitud, Longitud, Servicio_idServicio) values(?,?,?,?,?,?,?,?)", array($path.'1', $path.'2', $path.'3', $path.'4',$json-&gt;termino, $json-&gt;latitud, $json-&gt;longitud, $json-&gt;idServicio));
          $entradas = array(
                      'RutaFoto1' =&gt; $path.'1',
                      'RutaFoto2' =&gt; $path.'2',
                      'RutaFoto3' =&gt; $path.'3',
                      'RutaFoto4' =&gt; $path.'4',
                      'FechaTermino' =&gt; $json-&gt;termino,
                      'Latitud' =&gt; $json-&gt;latitud,
                      'Longitud' =&gt; $json-&gt;longitud,
                      'Servicio_idServicio' =&gt; $json-&gt;idServicio
                      );
          Detalle_Servicio::create($entradas);
  
         $array = array('Code' =&gt; '202', 'Message' =&gt; 'Done');
         return Response::json($array);
      } else {
          $array = array('Code');
         return Response::json('error', 400);
      }
  
  
   });
  </code></pre>
  
  <p><em>You can see that I get a JSON containing the values ​​that I store in the database</em></p>
  
  <p>I keep the data in the database of the <code>Detalle_Servicio</code> table but then I need to save some data in <code>Material _Usado</code> but I need the ID that was generated when you save the data in the table <code>Detalle_Servicio</code></p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>class Detalle_Servicio extends Eloquent{
  protected $table = 'Detalle_Servicio';
  protected $primaryKey = 'idDetalle_Servicio';
  protected $fillable = array('RutaFoto1', 'RutaFoto2', 'RutaFoto3', 'RutaFoto4', 'FechaTermino', 'Latitud', 'Longitud', 'Servicio_idServicio');
  
  
  public function servicio(){
      return $this-&gt;belongsTo('Servicio', 'idServicio'); //le pertenece a
  }
  public function material_usado(){
      return $this-&gt;hasMany('Material_Usado', 'idMaterial_Usado');
  }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Material_Usado extends Eloquent{
  protected $table = 'Material_Usado';
  protected $primaryKey = 'idMaterial_Usado';
  
  public function detalleServicio(){
      return $this-&gt;belongsTo('Detalle_Servicio', 'idDetalle_Servicio');
  }
  }
  </code></pre>
  
  <p>how can I do it?</p>