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



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2VgZkbw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire