Laravel 5.4 - use custom attribute model in collection
- I have a model
Order
. - The order has a lot
Item
. - An
Item
has an attributeprice
(from the database). -
Order
has agetAmountAttribute
method that looks like this:
/**
* @return double
*/
public function getAmountAttribute()
{
return $this->items->sum('price');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function items()
{
return $this->hasMany(Item::class);
}
So, I can get the total cost of the order items simply by doing $order->amount
.
Now I have a collection Order
and I want to get every order when the total price starts at 10. How can I achieve this since I cannot use my own attribute in the statement where
?
+3
source to share