products Table:
id name
1 first product
product_credits table:
id product_id quantity
1 1 10
2 1 20
What I am trying to do:
I am trying to get all the products quantities.
Expected data:
Get the:
- product name = first product
- product quantity = 30
Model Relationship code:
public function credits () {
return $this -> hasMany('App\Models\Product\ProductCredits')
-> whereHas('importInvoice', function ($query) {
$query->where('deleted_at', null);
}) -> orderBy('created_at', 'ASC') -> orderBy('quantity', 'DESC');
}
The relationship works good.
What I have tried:
I have tried to use with
function in the eloquent.
public function exportProductsCredit(Request $request) {
// GET THE COLLECTION
$products = Product::with(['credits' => function ($query) {
$query -> where('quantity', '>', 1) -> groupBy('product_id') -> sum('quantity');
}]) -> get();
return $products;
}
But it throw an error: product_credits.id' isn't in GROUP BY
EDIT
Problem is that I have Mutator
fields that I want to show for example I calculate the product_net_price
which I return as a mutator.