title: How to get the max ID in a Join Query in Laravel 5 (Integrity constraint violation:)
body: <p><strong>My query is like this:</strong></p>
  
  <pre><code>$deals=DB::table('leadsheet')
              -&gt;join('Deal', 'leadsheet.leadcode', '=', 'Deal.leadcode')
               -&gt;join('benefits', 'leadsheet.leadcode', '=', 'benefits.leadcode')
               -&gt;join('delegatedealinfo', 'leadsheet.leadcode', '=', 'delegatedealinfo.leadcode')
                -&gt;join('vipbooking', 'leadsheet.leadcode', '=', 'vipbooking.leadcode')
               -&gt;where('id', DB::raw("(select max(`id`) from vipbooking)"))
             -&gt;where('leadsheet.leadcat', '=','Delegates')
  
              -&gt;get();
  </code></pre>
  
  <p><strong>So I have following tables:</strong></p>
  
  <pre><code>1.leadsheet 
     -- leadcode
     -- leadcat
  2.Deal
     -- leadcode
  3.benefits
     -- leadcode
  4.delegatedealinfo
     -- leadcode
  5.vipbooking
     -- leadcode
  </code></pre>
  
  <p>What I'm trying to do is to get all the maximum id of <code>vipbooking</code> form where <code>leadcode</code> is same as all the <code>leadcode</code> FROM <code>leadsheet</code> WHERE <code>leadsheet</code>.<code>leadcat</code>=Delegates</p>
  
  <p>My only problem is <code>MAX ID</code> for <code>vipbooking</code> form is not working </p>
  
  <p>Can any one one help me out </p>
  
  <p><strong>UPDATE1</strong></p>
  
  <p>after applying the solution provided by @anant </p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from `leadsheet` inner join `Deal` on `leadsheet`.`leadcode` = `Deal`.`leadcode` inner join `benefits` on `leadsheet`.`leadcode` = `benefits`.`leadcode` inner join `delegatedealinfo` on `leadsheet`.`leadcode` = `delegatedealinfo`.`leadcode` inner join `vipbooking` on `leadsheet`.`leadcode` = `vipbooking`.`leadcode` where `id` = (select MAX(id) AS vipid from vipbooking) and `leadsheet`.`leadcat` = Delegates)
  </code></pre>
  
  <p><strong>Rephrasing Question</strong></p>
  
  <p>I want to get the details of the vipbooking form for a Deal of a Delegate user , we can find if a Deal is Vendor or Delegate from Leadsheet table  </p>
  
  <p>Leadsheet</p>
  
  <pre><code>id | leadcode | leadcat
  
  1  | DL2016012| Delegates
  2  | DL2016013| Delegates
  3  | VL2016001| Vendors
  4  | VL2016002| Vendors
  </code></pre>
  
  <p>Deals</p>
  
  <pre><code>id | leadcode | DealAmount
  
  1  | DL2016012| 123
  2  | VL2016002| 1000
  2  | DL2016013| 1200
  </code></pre>
  
  <p>vipbooking</p>
  
  <pre><code>    id | leadcode | date      | bookingtxt
  
      1  | DL2016012| 20-04-2016| xxx
      2  | DL2016012| 20-04-2016| dddd
      3  | VL2016012| 21-04-2016| ppp
      4  | DL2016013| 20-04-2016| xxx
      5  | DL2016013| 22-04-2016| dddd
  </code></pre>
  
  <p>So my Out put should have </p>
  
  <pre><code> 2 123 | Delegates| DL2016012| 20-04-2016| dddd
   5 1200| Delegates| DL2016013| 22-04-2016| dddd
  </code></pre>
  
  <p>Thanks</p>