mardi 8 septembre 2020

How Can i fetch all data with average rating in laravel

I want to display all the products with their average rating. In this case, I Have two tables like Product Table and Product Rating Table

For example:

My Products table

ProductID  | ProductName
-------------------------
1          | ABC Product
2          | XYZ Product
3          | LMN Product

My Rating Table

   ID   | ProductID  | Value
    -------------------------
    1   | 1          | 4
    2   | 1          | 5
    3   | 2          | 3

I need to get output like this,

 [
    {
     "ProductID": 1,
     "ProductName": ABC Product,
     "AverageRating": 4.5,
    },

    {
     "ProductID": 2,
     "ProductName": XYZ Product,
     "AverageRating": 3.0,    
    },  

   {
     "ProductID": 3,
     "ProductName": LMN Product,  
     "AverageRating": 0,
    }

 ]

How can I get this output in laravel? Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire