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).
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.
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.
Tables: products, users, alerts
alerts have a user_id
, a product_id
, and an amount
at which the user would like to receive an alert that the product is on sale under a certain price.
Currently I'm using a raw db query to get all the alerts:
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 <= p.saleprice1
My eloquent models are set up with relations as follows
User Model
public function alerts()
{
return $this->hasMany('Alert');
}
Product Model
public function alerts()
{
return $this->hasMany('Alert');
}
Alert Model
public function product()
{
return $this->belongsTo('Product');
}
public function user()
{
return $this->belongsTo('User');
}
Thanks for any and all suggestions. Matt