简体   繁体   English

雄辩的Laravel JSON响应

[英]Laravel JSON Response with Eloquent

I got three tables called threads,posts,users 我得到了三个表,分别是线程,帖子,用户

threads got 线程得到了

id
title
etc.

posts got 帖子得到了

id
thread_id
user_id
post
post_date
etc.

users got 用户得到了

id
username 
password
email
etc.

when users wants to view a thread i simply call a jquery ajax request using post data to send thread id and return this code : 当用户想要查看线程时,我只需使用发布数据调用jquery ajax请求来发送线程ID并返回以下代码:

Response::eloquent(Thread::find(Input::get('threadid'))->posts()->get());

this code does his job perfectly and return posts by same thread_id. 这段代码完美地完成了他的工作,并通过相同的thread_id返回帖子。 (post model also eager loads the user by looking user_id). (发布模型也渴望通过查找user_id来加载用户)。

however this code grabs every attribute of posts and users i mean everything to json, post_date, post_updatereason, user_password, user_email all columns in tables. 但是,这段代码抓住了帖子和用户的每个属性,我的意思是json,post_date,post_updatereason,user_password,user_email表格中所有列的所有内容。

I just want to grab post, username and post_date with json 我只想用json抓取帖子,用户名和post_date

You can add public static $hidden = array('list', 'of', 'fields'); 您可以添加public static $hidden = array('list', 'of', 'fields'); to your model which extends Eloquent. 扩展到Eloquent的模型。 Then none of the fields which you enumarate in the array will be returned. 然后,将不会返回您在数组中枚举的所有字段。 For more information refer to the documentation . 有关更多信息, 请参阅文档

For example 例如

class User extends Eloquent {
  public static $hidden = array('password', 'email');
}

will return the user model without the password and email . 将返回没有passwordemail的用户模型。

如果您使用的是laravel 3,并且版本4可能与此相同,则应该能够使用get方法来指定想要的列。

Response::eloquent(Thread::find(Input::get('threadid'))->posts()->get(['username','post_date']));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM