How can I print a Laravel Eloquent Model with null values? I want to get a new Question object to include all the fields, just with null
, but instead, it prints a []
.
var app = new Vue({
el: "#survey-form",
data: function() {
return {
survey: {!! $survey !!}
};
},
methods: {
add: function() {
return this.survey.questions.push({!! new App\Question !!});
}
}
});
Given that App\Question
has id
, survey_id
, and text
fields,
(new App\Question())->toJson()
prints []
.
How Can I make it print {"id": null, "survey_id": null, "text": null}
?
Edit
For those wondering, I'm trying to get the structure of a question so that I don't have to worry about changing the structure of the JSON in the template if the model structure changes. I know I could easily hardcode the structure with
return this.survey.questions.push({"id": null, "survey_id": null, "text": null});
but with that, every time a column is added to the questions table, I'll have to remember to change that in the template file too. By relying on {!! new App\Question !!}
, I hoped it would reflect the structure automatically.