mardi 25 décembre 2018

Fetch data with column alias from related table in laravel

There are two table

category id| name

Product id|category_id|name

id is foreign key for Category_id in Product table

When fetching data from these table using following query

        $results = Product::with('Category')->select('id as i')->paginate(10) ;

The data is received in the following format

current_page: 2
data: [{id: 3, name: "ssdfsdfasd"  , color: "ewr",…},…]
0: {id: 3,  name: "ssdfsdfasd",category:{name :"cat1" ,id:2} 
count: 3
But I need data in following format
0: {id: 3,  name: "ssdfsdfasd",category_name :"cat1" ,category_id:2} 

Tried using select as in above mentioned code but received similar response



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire