Code
$drillDownChart = Loan::select('loan_type AS name')
->groupBy('loan_type')
->where('transaction_year', $transyear)
->where('invalid', false)
->get();
$drillDownChart->map(function($val)use($loanChartArr) {
$val->id = $val->name;
$val->data = $loanChartArr;
return $val;
});
Outputvar_dump(json_encode($drillDownChart));
string(340) "[{"name":"Hospitalization","id":0,"data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]},{"name":"Salary","id":0,"data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]},{"name":"Emergency","id":0,"data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]}]"
The$val->id = $val->name;
should return "Hospitalization", "Salary" and "Emergency" but instead it returns 0. Notice if I change "id" to "ID", it shows the correct output.
$drillDownChart->map(function($val)use($loanChartArr) {
$val->ID = $val->name;
$val->data = $loanChartArr;
return $val;
});
Output:
string(373) "[{"name":"Hospitalization","ID":"Hospitalization","data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]},{"name":"Salary","ID":"Salary","data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]},{"name":"Emergency","ID":"Emergency","data":[[1,0],[2,0],[3,0],[4,0],[5,2],[6,1],[7,3],[8,1],[9,1],[10,0],[11,0],[12,0]]}]"
What is wrong in here? Is "id" restricted to be used in laravel in this case? Please help. Thank you.
By default, Laravel assumes your model has a primary key namedid
with a value of typeint
.
Therefore, theid
attribute is casted to anint
. It is like having this default casts:
protected $casts = [
'id' => 'int'
];
So when you accessid
, yourstring
(e.g. "Hospitalization") is casted toint
. The result is0
.
If you really want to keep your current code, you'll have to do some hacky stuff, like setting a accessor for theid
attribute:
public function getIdAttribute($value)
{
return $value;
}
This way, the cast will be overriden and you will be able to retrieve the name the way you want.
However, I would advice against manually changing theid
at runtime, it is obviously a code smell, a sign that something isn't quite right with this part of your project.
For those who like digging further, this is where all what I explained happens:
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
/**
* Get the casts array.
*
* @return array
*/
public function getCasts()
{
if ($this->getIncrementing()) {
return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts);
}
return $this->casts;
}
When you try to access an attribute ($this->id
), Laravel will determine if it has an accessor, if this is a relationship or not... and at the end, will cast it to the correct type.
To get the casts, it will call->getCasts()
.
First this method will check if your model is auto incrementing (which can be disabled by setting the property$incrementing
tofalse
).
If that's the case, it will get the primary key name (->getKeyName()
which isid
by default) and its type (->getKeyType()
which isint
by default).
This key pair will be merged with the$casts
property of the model.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.