title: How to update 2 table data using query builder
body: <p>I have problem with <strong>edit and update</strong> with 2 table using query builder.
  --->After i press button submit--->the data insert new row---> not update current data.</p>
  
  <p>This is my function edit(only for view old data)</p>
  
  <pre><code>public function edit(Request $request, $id){
      $tax_rate = TaxRate::find($id);
      $tax_rate_details = TaxRateDetail::where('tax_rate_id', $id)-&gt;get();
      $country = Country::all();
      $geo_zones = GeoZone::all();
      //dd($tax_rate);
      //dd($tax_rate_details);
      //dd($geo_zones);
      if(!$tax_rate) {
  
         return redirect('/');
       }
      return view('tax_management.edit',['country' =&gt; $country , 'tax_rate'=&gt;$tax_rate , 'geo_zones'=&gt;$geo_zones, 'tax_rate_details'=&gt;$tax_rate_details]);
    }
  </code></pre>
  
  <p>This is my update(i do validation and the saveTax is the saving part)</p>
  
  <pre><code>public function update(Request $request, $id){
      $this-&gt;validate($request,[
        'country_id'=&gt; 'required',
        'tax_type' =&gt; 'required',
        'name' =&gt; 'required|max:100',
        'code' =&gt; 'required|max:50'
      ]);
      //dd($request-&gt;input());
      DB::beginTransaction();
  
      try{
        $tax_rate = TaxRate::find($id);
        $tax_rate_details = TaxRateDetail::where('tax_rate_id', $id)-&gt;get();
  
        $this-&gt;saveTax($request, $tax_rate);
  
        DB::commit();
  
        return redirect()-&gt;route('tax_management.index');
      } catch (\Exception $ex){
        //dd($ex);
        DB::rollback();
        return back()-&gt;withInput()-&gt;withErrors('Fail to save');
      }
    }
  </code></pre>
  
  <p>This is my function save()</p>
  
  <pre><code>private function saveTax(Request $request, $tax_rate){
      $tax_rate-&gt;country_id = $request-&gt;input('country_id');
      $tax_rate-&gt;geo_zone_id = $request-&gt;input('geo_zone_id');
      $tax_rate-&gt;tax_type = $request-&gt;input('tax_type');
      $tax_rate-&gt;name = $request-&gt;input('name');
      $tax_rate-&gt;code = $request-&gt;input('code');
      $tax_rate-&gt;description = $request-&gt;input('description');
      if(!empty($request-&gt;input('active'))){
        $tax_rate-&gt;active =1;
      } else {
        $tax_rate-&gt;active =0;
      }
      $tax_rate-&gt;save();
  
      if($tax_rate-&gt;tax_rate_id) {
        TaxRateDetail::where('tax_rate_id', $tax_rate-&gt;tax_rate_id)-&gt;delete();
      }
      if($request-&gt;input('tax_rate_details')){
        foreach ($request-&gt;input('tax_rate_details') as $key =&gt; $value) {
          $tax_rate_detail = new TaxRateDetail();
          $tax_rate_detail-&gt;tax_rate_id = $tax_rate-&gt;tax_rate_id;
          $tax_rate_detail-&gt;priority = $value['priority'];
          $tax_rate_detail-&gt;date_from = $value['date_from'];
          $tax_rate_detail-&gt;date_to = $value['date_to'];
          $tax_rate_detail-&gt;rate = $value['rate'];
          $tax_rate_detail-&gt;type = $value['type'];
          $tax_rate_detail-&gt;active = $value['active'];
          //dd($tax_rate_detail);
          $tax_rate_detail-&gt;save();
        }
      }
    }
  </code></pre>
  
  <p>I want to save edit update with old(id). not create new. Please help thank you. I don't know where the code gone wrong.</p>