I have values returned that way after a calculation to take the percentage.
$school = $api->schools;
foreach ($school as $key => $value){
$name = $value->id;
$data = DB::table('sys_users')
->where('schoolid','=',$name)
->get();
$exist1 = count($data)*4;
$datarecord = DB::table('sys_record')
->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
->where('sys_users.schoolid','=',$name)
->get();
$correct1 = count($datarecord);
function porcentagem_nx($parcial,$total) {
return ($parcial * 100) / $total;
}
$worldone[$value->id] = porcentagem_nx($correct1, $exist1);
// Because you already used $data above, I chose a different name
// for this array.
$items[] = [
"id" => $value->id,
"name" => $value->name,
"worldone" => $worldone,
];
}
dd($items);
I will use it on the datatable so it is necessary for my manual tests to be like the one below, but dynamic.
$data = [
[
"id" => 1,
"name" => "Brand",
"worldone" => $worldone,
],
[
"id" => 2,
"name" => "Just",
"worldone" => $worldone,
],
];
I'm having trouble bringing the information from the worldone variable out of the foreach and making the array dynamic for all array records in the variable $school.
$dataarray that you're trying to achieve? Are they coming from the$valueobject in theforeachloop?porcentagem_nx()is being declared twice somewhere. Is it declared in a Controller in the same namespace?App\Http\Controllers\RelatoriosController.phpand somewhere else? If you need a second declaration, perhaps you could rename it to something likeporcentageem_nx2()and see if it clears up the error?