I have a RESTful API in laravel 4 and I have the following command
return News::with('User')->get(array('id', 'body', 'created_at', 'categorie_id', 'user_id'))->find(1)->toJson();
and it return:
{"id":"1","body":"hola a todos", "created_at":"2014-09-11 17:18:01","categorie_id":"3","user_id":"1","user":{"id":"1","nick":"angel","description":null,"photo":null,"origin":"España","created_at":"2014-09-11 17:18:00","updated_at":"2014-09-11 17:18:00"}}
It return the selected columns that I have chosen from "News" (id, body, relevancia, lat, lng, created_at, categorie_id, user_id) It is ok, but I also want select columns from "User", in the example I don't want that json return updated_at (from "user")
How can I do that?
Thanks
Just add a hidden property in your User model like:
protected $hidden = array('updated_at');
That's it. You may add more fields in the array to exclude from your json result. Also you may try something like this (Instead of hidden property):
return News::with(array('User' => function($q) {
$q->select('username', 'email'); // select fields from user table
}))->get(array('id', 'body');
This way you don't have to add the hidden property but you may specify the fields you want to select from your users table so you may exclude the field from query but by default it's not hidden.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With