Friday, March 30, 2018

extract values from a method Laravel - development

extract values from a method Laravel

I have to add columns to a collection based on a common key …

$usersDatas = $users->each(function ($record) use ($timeData) {
    $times = array_first($timeData, function ($value, $key) use ($record) {
            return $value['id'] === $record['id'];
    });

    $record['totalTime'] = $times['totalTime'];
    $record['spendTime'] = $times['spendTime'];
    $record['remainingTime'] = $times['remainingTime'];
    var_dump($record);
 });
 dd($usersDatas);

the dd($records) inside gives me the right result … but i can’t extract the results outside…

the dd($records) gives me :

array:17 [▼
  "id" => 1
  "ua_id" => 351
  "grade_id" => 584
  "metier_id" => 18
  "nom" => "XXX"
  "prenom" => "XXX"
  "matricule" => "XXX"
  "email" => null
  "datenaissance" => "XXX"
  "casque" => null
  "created_at" => "2018-01-30 09:59:32"
  "updated_at" => "2018-01-30 09:59:32"
  "deleted_at" => null
  "totalTime" => 240
  "spendTime" => 0
  "remainingTime" => 0
]

While dd($usersDatas) gives me this :

0 => array:14 [▼
    "id" => 1
    "ua_id" => 351
    "grade_id" => 584
    "metier_id" => 18
    "nom" => "XXX"
    "prenom" => "XXX"
    "matricule" => "XXX"
    "email" => null
    "datenaissance" => "XXX"
    "casque" => null
    "created_at" => "2018-01-30 09:59:32"
    "updated_at" => "2018-01-30 09:59:32"
    "deleted_at" => null
    "pivot" => array:5 [▶]

the 3 last fields are missing…

"totalTime" => 240
"spendTime" => 0
"remainingTime" => 0



from Laravel Questions and Answers https://laravelquestions.com/laravel/extract-values-from-a-method-laravel/
via Lzo Media

No comments:

Post a Comment