Public variable loses all nested data on render
Hi´!, I´m working in laravel 8 and Livewire 2.5.
I am creating a query where the users select what he wants from some levels, so I have created this and it's working perfectly then I aslo wanted to export this data to Excel and that's ready too, but the issue is when clicking the button to export the data because the public variable which has the data loses all nested data so it throws and error because theres is nothing to query.
The code it's quite long so I'm just pasting part of the code.
This is the "query" I'm generating, in few words it is just selecting all projects and it indicator to show the progress of them and the progress of the indicator
public function searchIndicator($level, $indicator)
{
$institution_id = $this->ins_tmp ? $this->ins_tmp->id : ($this->institution ? $this->institution : get_institution()->id);
$this->avg_por_ind = 0;
if ($level) {
$ind = $level[0] ? $level[0]->indicator : $level->indicator;
} else if ($indicator) {
$ind = $indicator;
}
$ind = $ind->each(function ($indicator, $key) use ($institution_id) {
$pin = ProjectIndicator::select('project_indicators.*')
->join('projects', 'projects.id', '=', 'project_indicators.project_id')
->join('teams', 'teams.id', '=', 'projects.team_id')
->join('institutions', 'institutions.id', '=', 'teams.institution_id')
->where('project_indicators.indicator_id', $indicator->id)
->where('institutions.id', $institution_id)->get();
$porcentaje = $pin->count() ? 100 / $pin->count() : 0;
$this->avg_por_pro = 0;
$pin = $pin->each(function ($proin, $key1) use ($porcentaje) {
if ($this->option === 'proyectos') {
$pro = $proin->project;
$pro->accumulated_progress = $pro->project_trackings()->sum('progress_current_goal');
$pro->percentage_given = $porcentaje;
$pro->contribute_percentage = ($porcentaje * $pro->accumulated_progress) / 100;
$this->avg_por_pro += $pro->contribute_percentage;
} else {
$proin->project;
$accp = $proin->project_indicator_trackings()->sum('progress_goal');
if ($proin->goal_type) {
if ($accp >= $proin->goal) {
$proin->accumulated_progress = 100;
} else {
$proin->accumulated_progress = ($accp * 100) / $proin->goal;
}
$proin->sum_unities = $accp;
} else {
$proin->accumulated_progress = $accp;
}
$proin->percentage_given = $porcentaje;
$proin->contribute_percentage = ($porcentaje * $proin->accumulated_progress) / 100;
$this->avg_por_pro += $proin->contribute_percentage;
}
});
$indicator->accumulated_progress = $this->avg_por_pro;
$indicator->contribute_percentage = ($indicator->percentage * $this->avg_por_pro) / 100;
$indicator->project_indicator = [];
$indicator->project_indicator = array_merge($indicator->project_indicator, $pin->toArray());
$this->avg_por_ind += $indicator->contribute_percentage;
});
return $ind;
}
The result of this is being saved on a public function to then query through the view and show it to the user, here the $zero var contains some more logic.
$this->result = $zero;
At the enda this is what I'm getting
#attributes: array:13 [▼
"id" => 1
"institution_id" => null
"zero_level_id" => "AD"
"zero_level_title" => "Agenda Digital"
"slug" => "ad"
"active" => 1
"end_date" => null
"eliminated_at" => null
"removal_reason" => null
"created_at" => "2022-06-09 10:17:38"
"updated_at" => "2022-06-09 10:17:38"
"institutions" => array:1 [▼
0 => array:21 [▼
"id" => 1
"department_id" => 6
"municipality_id" => 207
"institution_id" => "12345"
"institution_name" => "Autoridad de Aviación Civil"
"acronym" => "AAC"
"institution_landline_phone_number" => 25654400
"institution_mobile_phone_number" => 12345678
"institution_email" => "[email protected]"
"address" => "Carretera Panamericana Km 9 1/2, Contiguo a Zona Franca San Bartolo"
"start_date" => "2022-06-09"
"logo_url" => "institution-logo/aac_logo.png"
"slug" => "autoridad-de-aviacion-civil"
"active" => 1
"end_date" => null
"eliminated_at" => null
"removal_reason" => null
"created_at" => "2022-06-09 10:16:51"
"updated_at" => "2022-06-09 10:16:51"
"level_one" => Illuminate\Database\Eloquent\Collection {#1951 ▶}
"contribute_percentage" => 0.5078125
]
]
"accumulated_progress" => 0.5078125
]
AS seen on the result there is some nested data and then more and so on, everythings Ok but when trying to export the data the variables wich contains return this.
#attributes: array:11 [▼
"id" => 1
"institution_id" => null
"zero_level_id" => "AD"
"zero_level_title" => "Agenda Digital"
"slug" => "ad"
"active" => 1
"end_date" => null
"eliminated_at" => null
"removal_reason" => null
"created_at" => "2022-06-09 10:17:38"
"updated_at" => "2022-06-09 10:17:38"
]
So there is no nested data and I do not know why it loses the nested data because in the render method I'm just returning the view so nothing is rendering to think that there is the issue
public function render()
{
return view('livewire.levels.percentage.completion-percentage');
}
Hope some one can help me.
Please or to participate in this conversation.