I would expect the responding array to contain the Appends attribute. In this case the pin.
For instance the the setAppends works as follows:
$account = Account::find(1);
$account->setAppends(array('pin'));
$account->toJSON();
And it's result something like (includes pin because of setAppends):
{
"name": "Account1",
"type": "blue",
"pin": 1234
}
Now the append through a relationship (or eager load) ideally would be able to also use setAppends:
$user = User::find(1);
$user->setAppends(array('pin'));
/* or $user->account->setAppends(array('pin')); */
$user->toJSON();
And it's expected result something like:
{
"first_name": "Bob",
"last_name": "Hope",
"account": {
"name": "Account1",
"type": "blue",
"pin": 1234
}
}
However the 'pin' doesn't get included as expected as hoped.
At the time of this writing the only way of doing what you want is manually.
$account = Account::find(1);
$account->pin = $account->pin;
$account->toJSON();
Since then I made a commit to the framework adding the append()
method.
Now you use it like this:
$account = Account::find(1)->append('pin')->toJSON();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community