With () and Get () in eloquent laravel 4
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 returns:
{"id": "1", "body": "hola a todos", "created_at": "2014-09-11 17:18:01", "categorie_id": "3", "user_id": "one "," 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 returns the selected columns that I selected from "News" (id, body, actualancia, lat, lng, created_at, categorie_id, user_id) This is fine, but I also want the column "User" For example, I don't want json returned update_at (from "user")
How can i do this?
thank
source to share
Just add a property hidden
to your model User
, for example:
protected $hidden = array('updated_at');
What is it. You can add more fields to array
to exclude from your result json
. Also you can try something like this (instead of a property hidden
):
return News::with(array('User' => function($q) {
$q->select('username', 'email'); // select fields from user table
}))->get(array('id', 'body');
This way you do not need to add a property hidden
, but you can specify the fields you want to select from the table users
to exclude the field from the query, but it is not hidden by default.
source to share