vendredi 20 avril 2018

Laravel API Routes manipulate the default JSON return

Let's say I have 2 tables. users & books. The tables look like this users:

id, name
1, Jone
2, Doe

books:

id, name, user_id
3, Dummy book1, 1
4, Dummy book2, 1
5, Dummy book3, 2

When I try to return all the books belong to a user (ex. user 1) like this

$query->where('id', 1)->join('books', 'users.id', '=', 'books.user_id');

I get JSON, BUT the issue is JSON display id for books not the user like this:

id: 3,//this is the book id not the user id
user_id: 1
...
...
id: 4,
user_id: 1

How I can make my query display the id as the user id like this

id:1
... whatever

Better yet, how I can manipulate the whole JSON so I can rename the keys? For example books.name to books_name and users.name to user_name I don't like the default JSON return

One of the reason I ask this that's if you join 2 tables or more the id will always be the last table.id



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hi4dJU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire