1

I have an array of array which results from several sql requests. First I make a sql request in a foreach in order to have all the datas i need

foreach ($users as $user) {
   // Two different Eloquent requests 
   $todo = User::select().....where('users.id' = $id);
   $done = User::select().....where('users.id' = $id);

   // I have operation on Collection that work fine ... 
   $totalTimes = $todo->toBase()->sum('dureeformation') ;
   $spendTimes = $done->toBase()->sum('dureeformation') ;
   $remainingTimes = $totalTimes - $spendTimes;

  $all[] = ['user_id' => $id, 'totalTime' => $totalTimes, 'spendTime' => $spendTimes,'remainingTime' => $remainingTimes ];

}

My issue is the following... Outside the foreach i display the values and i have this array of array ...

array:16 [▼
  0 => array:4 [▼
    "user_id" => 1
    "totalTime" => 465
    "spendTime" => 0
    "remainingTime" => 465
  ]
  1 => array:4 [▼
    "user_id" => 3
    "totalTime" => 375
    "spendTime" => 0
    "remainingTime" => 375
  ]

I would need to have a Collection instead ... I tried to make $all = Collect(....) but i doesn't give me the expected result.

I need a collection because i have to create a Collection with this created collection and another one from another request.

Concerning this part i already had this case and i can solve it.

Thanks for your help

1
  • You said you tried $all = Collect(....) why don't you convert the result like $result = collect($all) Commented Mar 28, 2018 at 11:15

1 Answer 1

6

try this helper function :

$collection = collect($all);

This function will convert your array to collection.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried collect but not this way :) i feel much more better now !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.