title: Using Eloquent in Laravel for more advanced DB queries
body: <p>I've been developing a laravel application the last couple months and it's my first ride with the framework.  It's also my first time embracing and using an ORM (Eloquent).</p>
  
  <p>For the most part, Eloquent seems pretty straight forward when doing basic DB operations.  However when things get a little more complicated I often find myself spending way too much time trying to figure out how do it the Eloquent way and jump ship to raw db queries.</p>
  
  <p>Was hoping someone might suggest whether or not my desired outcome in the more slightly complicated queries such as the one below should be achievable with Eloquent rather easily, or that I should just keep using raw DB queries.</p>
  
  <p>Tables: <strong>products</strong>, <strong>users</strong>, <strong>alerts</strong></p>
  
  <p><strong>alerts</strong> have a <code>user_id</code>, a <code>product_id</code>, and an <code>amount</code> at which the user would like to receive an alert that the product is on sale under a certain price.</p>
  
  <p>Currently I'm using a raw db query to get all the alerts:</p>
  
  <pre><code>SELECT p.*, u.firstname, u.lastname, u.email, a.amount AS alert_amount
  FROM alerts AS a                                       
  JOIN dods AS d ON a.product_id = d.product_id                                        
  JOIN products AS p ON d.product_id = p.id                                        
  JOIN users AS u ON u.id = a.user_id
  WHERE a.amount &lt;= p.saleprice1
  </code></pre>
  
  <p>My eloquent models are set up with relations as follows</p>
  
  <p><strong>User Model</strong></p>
  
  <pre><code>public function alerts()
  {
      return $this-&gt;hasMany('Alert');
  }
  </code></pre>
  
  <p><strong>Product Model</strong></p>
  
  <pre><code>public function alerts()
  {
      return $this-&gt;hasMany('Alert');
  }
  </code></pre>
  
  <p><strong>Alert Model</strong></p>
  
  <pre><code>public function product()
  {
      return $this-&gt;belongsTo('Product');
  }
  
  public function user()
  {
      return $this-&gt;belongsTo('User');
  }    
  </code></pre>
  
  <p>Thanks for any and all suggestions.
  Matt</p>