4
Hello, I’m using Laravel 5.1 at the moment and I came up with a question. I have a grid that shows values of a given CRUD and I search through Table::all(), which returns something like:
array:5 [▼
"primary_key_id" => 1,
"foreing_key_id" => 5,
"name" => "testes",
"created_at" => "2015-10-05 21:25:27",
"updated_at" => "2015-10-05 21:25:27"
I would like to add in this object the name of foreing_key, ie:
array:6 [▼
"primary_key_id" => 1,
"foreing_key_id" => 5,
"name" => "testes",
"created_at" => "2015-10-05 21:25:27",
"updated_at" => "2015-10-05 21:25:27",
**"foreing_key_name = "Teste"**
I tried to use the Laravel Mutators concept and through the print I did inside the method actually added what I wanted, but I can’t access the view or anywhere, and it doesn’t appear inside the object inside the controller. I am using the Mutators concept correctly or I will have to do a JOIN to solve this?
Example of the mutator:
getForeingKeyIdAttribute($value){
$this->attributes['foreing_key_name'] = TableDaForeinkey::where('name','=', $value )->first()->name;
}
Publish your models if possible. This helps to reproduce your question.
– gmsantos
I don’t think it’s a good idea.
– Wallace Maxters