Reputation: 372
I have a mutator
that calculate the product existing quantity by:
summing the product credit and minus it from the sold credit.
public function getReportTotalQuantityAttribute() {
// GET IMPORT INVOICE SUM
$import_invoices_sum = $this -> credits -> sum('quantity');
// GET EXPORT INVOICE SUM
$export_invoices_sum = $this -> sold -> sum('quantity');
// CALCULATE THE SUM
return $import_invoices_sum - $export_invoices_sum;
}
This mutator
works fine and return the actually product quantity as report_total_quantity
attribute whenever I call the model.
I am trying to get the product where('report_total_quantity', '>', 0)
.
$condition_queries = Product::withCreditsAndWarehouses() -> where('report_total_quantity', '>', 0);
But I am getting error say Unknown column 'report_total_quantity'
. And this is logical since I don't have this column but I append it using $append
and mutator
.
I have found something called filters
but I don't think it is good solution since I am using paginate(10)
and like that I will return 10
values and filter them.
Also I have many other conditions and filter is not working good for me.
Upvotes: 2
Views: 3444
Reputation: 2656
If you want to use WHERE
with attribute create by mutator
You must first create Collection
then use Where
statement.
$condition_queries = Product::withCreditsAndWarehouses()->get()->where('report_total_quantity', '>', 0);
But after ->get()
you can not use paginate
. Instead you must use Collection
paginate method forPage
. Check this
Upvotes: 2