0

i looping 2 array a date and the data

        $rdate = $request->month; //<-- Requested month
        $days = Carbon::create(); 
        $days = $days->daysInMonth; //<-- Finding how much days in month

        for ($i = 1; $i < $days; $i++) { //Looping Date
            $date = $rdate . '-' . $i; //Making full Date with formate 'Y-m-d'
            foreach ($games as $game) { //Looping game array have 3 collection ['game1', 'game2', 'game3']
                $result = Result::whereDate('created_at', $date)->where('game', $game->name)->where('admin_id', $admin->id)->first(); // Query Results for getting perticular game with date
                if ($result) { //Checking if current date haven't result
                    $r[] = [
                        'game' => $game->name,
                        'number' => $result->number
                    ];
                } else {
                    $r[] = [
                        'game' => $game->name,
                        'number' => '-'
                    ];
                }
            }

            $resultd[] = [
                'date' => $date,
                'results' => $r // i want to stop adding old data here
            ];
        }

i am expecting this result

{"results":[{"date":"2020-08-1","results":[{"game":"game1","number":"-"},{"game":"game2","number":"-"},{"game":"game3","number":"-"}]},{"date":"2020-08-2","results":[{"game":"game1","number":"-"},{"game":"game2","number":"-"},{"game":"game3","number":"-"}]

What actually getting

Adding old $r to results array

How to fix it i am trying to break it but can't figure it out

3
  • i solve the problem but still i want better solution Commented Aug 16, 2020 at 20:50
  • you did not have the "What actually getting" result. Commented Aug 16, 2020 at 21:21
  • I don't really understand your problem, but if you want to stop a loop use "break". Commented Aug 16, 2020 at 21:53

1 Answer 1

1

I have some untested code, as I do not have your data but I think this code below should work, you may need to modify some code as needed.

        $rdate = '2020-8';
        
        $days = collect([1,2,3]);
        
        $games = collect(['game1', 'game2', 'game3']);
        
        return $days->map(function ($item) use ($rdate,$games) {
            $date = $rdate.'-'.$item;
            
            return [ 
             "date" => $date,
             "results" => $games->map(function ($g) use ($date) {
                 
                 $result = Result::whereDate('created_at', $date)
                                   ->where('game', $g->name)
                                   ->where('admin_id', $admin->id)
                                   ->first();
                                   
                 return ['game' => $g->name,
                         'number' => $result ? $result->number : '-',
                 ];
             })
         ];   
        });
Sign up to request clarification or add additional context in comments.

Comments

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.