php - Laravel 5.2 从 json 响应中排除外键关系中的列

标签 php laravel laravel-5

我想从问题中排除question_type_id。请帮助我。

[
 {
  "template_id": 2,
  "title": "news surey2345",
  "instructions": " this is news surey2345",
  "reference": "a426a9288424ff3d540115ffc38bd0b4",
  "status_id": 1,
  "published_date": "2017-03-30 11:12:48",
  "expiry_date": null,
  "survey_template": {
  "is_public": 0,
  "submission_type": 1,
  "id": 2
},
"questiongroup": [
  {
    "survey_id": 17,
    "title": "question group 2",
    "instructions": " this is question group 2",
    "has_free_text": 1,
    "sort": 4,
    "id": 19,
    "question": [
      {
        "id": 58,
        "question_type_id": 1,
        "question_group_id": 19,
        "title": "question3",
        "can_upload_file": 0,
        "can_comment": 0,
        "include_na_answer": 1,
        "sort": 31,
        "question_answers": [
          {
            "question_id": 58,
            "answer": "answer1",
            "points": "12",
            "id": 206
          },
          {
            "question_id": 58,
            "answer": "answer2",
            "points": "11",
            "id": 207
          },
          {
            "question_id": 58,
            "answer": "NA",
            "points": "0",
            "id": 208
          }
        ],
        "question_type": {
          "slug": "multi-choice",
          "id": 1
        }
      },
      {
        "id": 134,
        "question_type_id": 1,
        "question_group_id": 19,
        "title": "question3",
        "can_upload_file": 0,
        "can_comment": 0,
        "include_na_answer": 1,
        "sort": 33,
        "question_answers": [],
        "question_type": {
          "slug": "multi-choice",
          "id": 1
        }
      },
      {
        "id": 161,
        "question_type_id": 1,
        "question_group_id": 19,
        "title": "question3",
        "can_upload_file": 0,
        "can_comment": 0,
        "include_na_answer": 1,
        "sort": 34,
        "question_answers": [],
        "question_type": {
          "slug": "multi-choice",
          "id": 1
        }
      },
      {
        "id": 165,
        "question_type_id": 1,
        "question_group_id": 19,
        "title": "question3",
        "can_upload_file": 0,
        "can_comment": 0,
        "include_na_answer": 1,
        "sort": 35,
        "question_answers": [],
        "question_type": {
          "slug": "multi-choice",
          "id": 1
        }
      }
     ]
     }
    ]
   }
 ]

最佳答案

class MyModel extends Model {
    protected $hidden = ['question_type_id'];
}

您可以向 $hidden 属性添加属性,以防止模型转换为 JSON 时显示该属性。

关于php - Laravel 5.2 从 json 响应中排除外键关系中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43118058/

相关文章:

PHPExcel 将零输出为空白单元格

php - 无法使用php连接到远程数据库

php - 如何使用 laravel eloquent 获取加载关系的外键名称

laravel-5 - Laravel 5 多连接中的多态

Laravel 5.5 Try/Catch 不工作它执行异常句柄

php - 在 html 输入值中获取 php date() 会破坏它

php - 尝试注册示例用户时出现 pdo 注册页面错误

php - 如何查找同一天同一列的两个日期之间的差异?

Laravel 迁移将列类型从 varchar 更改为 longText

php - 如果用户请求子域 WP,则重定向到子域