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

时间:2017-03-30 12:35:27

标签: php laravel laravel-5.2

我想从问题中排除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
        }
      }
     ]
     }
    ]
   }
 ]

1 个答案:

答案 0 :(得分:2)

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

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