komal Thakkar
komal Thakkar

Reputation: 21

Laravel 5.2 exclude column from the json response which is in foreign key relationship

I want to exclude question_type_id from question. Please help me.

[
 {
  "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
        }
      }
     ]
     }
    ]
   }
 ]

Upvotes: 2

Views: 1673

Answers (1)

Jono20201
Jono20201

Reputation: 3205

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

You can add attributes to the $hidden property to prevent it been shown when a model is converted to JSON.

Upvotes: 3

Related Questions