vendredi 8 mai 2020

Eloquent laravel : how count data from json column

Hi this is Products table

id | name      | price 
22 | product_1 | 10.00
33 | product_2 | 10.00
44 | product_3 | 10.00

and this Orders table.

in cartproducts column i have use json.

id|                 cartproducts                | summ 
1 | [{"productid":22,"quantity":1,"options":"[]"}] | 10.00
2 | [{"productid":33,"quantity":1,"options":"[]"},{"productid":44,"quantity":2,"options":"[]"}] | 30.00
3 | [{"productid":22,"quantity":3,"options":"[]"}] | 30.00

Using eloquent laravel how can i count how much order i have for each productid in Orders table using groupby method or other methods.

this is a simple imagine

$productorders = \App\Orders::where('productid',$productid)->groupBy('??')->orderby('count', 'desc')->get();

Desired output ( i will use my output in table sorted by Orders count )

product name | orders count | total earning | total quantity
product_1    |       2      |       40      |       4
product_2    |       1      |       10      |       1
product_3    |       1      |       20      |       2

thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire