title: How can I unscope a model on create in laravel
body: <p>I have a Has and Belongs To Many relationship between leads and statuses. A Lead is a prospective customer. The statuses can be active, on hold, unsubscribed, etc. I have the following tables:</p>
  
  <pre><code>| Leads   | Lead_Status  | Statuses |
  |---------|--------------|----------|
  |  - id   |  - lead_id   |  - id    |
  |  - name |  - status_id |  - name  |
  </code></pre>
  
  <p>We have multiple statuses for each lead because we need to keep track of the status history for analytics. This is all working great. It is common to apply a <a href="http://laravel.com/docs/4.2/eloquent#global-scopes" rel="nofollow">global scope</a> as opposed to <a href="http://laravel.com/docs/4.2/eloquent#query-scopes" rel="nofollow">query scope</a> to a model if the scope is used in most of the queries. My scope does something ugly to retrieve the current status of the lead and make it easily accessible with something like <code>$lead-&gt;status</code> that same way we get <code>$lead-&gt;name</code>. Here is the scoping process using newQuery to add query info to the initial query when getting the <code>Lead::</code>:</p>
  
  <pre><code>public function newQuery() {
    return parent::newQuery()-&gt;select(DB::raw('leads.*, status.status_id AS status'))-&gt;from(DB::raw(' leads
      LEFT OUTER JOIN (
        SELECT ls.status_id, ls.lead_id
        FROM (
           SELECT lead_id, max(created_at) AS max_created_at
           FROM lead_status GROUP BY lead_id
        ) AS ls2 INNER JOIN lead_status AS ls ON ls.lead_id = ls2.lead_id AND ls.created_at = ls2.max_created_at
      ) AS status ON leads.id = status.lead_id
    '));
  }
  </code></pre>
  
  <p>This works really well. It returns the lead with id, name and status. </p>
  
  <p>Now for <strong>the problem</strong>:</p>
  
  <p>When I try to create a new Lead I get an error:</p>
  
  <pre><code>SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 
  '
    LEFT OUTER JOIN (
      SELECT ls.status_id, ls.lead_id
      FROM (
  ' at line 2 
  (SQL: 
    insert into  leads
    LEFT OUTER JOIN (
      SELECT ls.status_id, ls.lead_id
      FROM (
        SELECT lead_id, max(created_at) AS max_created_at
        FROM lead_status GROUP BY lead_id
      ) AS ls2 
      INNER JOIN lead_status AS ls ON ls.lead_id = ls2.lead_id 
      AND ls.created_at = ls2.max_created_at
    ) AS status ON leads.id = status.lead_id
    (`id`, `name`, `updated_at`, `created_at`) values (test5, Dustin Griffith, 2014-12-03 18:24:06, 2014-12-03 18:24:06)
  )
  </code></pre>
  
  <p>It is obvious that the Lead is being scoped on <code>Lead::create()</code>. This is not what I want. I want it to scope only when we are selecting data from the leads table. So my main question is how do I tell this to only scope when it is a select statement? </p>
  
  <p>I have also used the <code>ScopeInterface</code> method seen in the laravel docs under <a href="http://laravel.com/docs/4.2/eloquent#global-scopes" rel="nofollow">global scope</a> which is more complex but favored in some situations. I can add that if we should be building off of that instead of the older <code>newQuery</code> method. I had the same results with that method.</p>
  
  <p>I am not looking for a hacky solution. Any input will be greatly appreciated but I am searching for the correct way to handle these situations because I will be doing this time and time again in the future.</p>