Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

musqsim's avatar

Create collection to print a map of month days and some values on days with values

So, I'm trying to iterate a foreach on blade template with days of month from 1 to current, and then have a second column with values if a relationship model has values on that day, if not, empty or null...

Lets say a User has Lessons, he goes to some lessons. So I want a collection(?) that has all days of month to current and if user had attended to a lesson, his rating for exemple.

My problem here is: Is it better to create these on the controller? Or directly in view? Where it will be more performant?

Should I use relationship? or should I check the date and get from collection if has value and push it?

Should I create a collection, or a plain PHP array?

Mnany thnaks for those who can help me get started with this!

0 likes
10 replies
musqsim's avatar

What I have done so far, but I thing i get the collection and not the value on "rating" value.

$user = User::where('id', $request->id)->with('lessons')->first();

    $a = 1;
    $collection = new Collection();
    while ($a <= date('d')) {
                $date = new Carbon(date('Y').date('m').$a);
                $collection->push((object)['date' => $a,
                                        'rating'=> $user->lesson->where('date', $date->toDateString())->first() ? $user->lesson->where('date', $date->toDateString())->first()->get('rating') : null

                ]);
                $a++;
            }
1 like
vincent15000's avatar

@musqsim THe code that you have just posted will send a query to the database for each date, according to me it's really not the good solution. Sure you can change this code and write a query to retrieve the right data from a unique query to the database. Then it's always possible to map into the retrieved collection to send the results with a specific needed format.

vincent15000's avatar

Is it better to create these on the controller? Or directly in view? Where it will be more performant?

It will be more performant if you create this inside the controller (or via a service or any other class, but not in the view).

Should I use relationship? or should I check the date and get from collection if has value and push it?

I don't understand what could be the relationship between a date and a course. According to me, it depends on what you need, but it would be relevant to retrieve the right datas directly from the database. It will always be more performant than handling a collection.

Should I create a collection, or a plain PHP array?

It will be more easy to handle a collection than a PHP array.

musqsim's avatar

Hi @vincent15000 , many thnaks for your support!

For helping on this: I'm trying to see a detail of a student, thats why the relationship between User and Lessons.

I just don't understand why this:

'rating'=> $user->lesson->where('date', $date->toDateString())->first() ? $user->lesson->where('date', $date->toDateString())->first()->get('rating') : null

return collection with 4 entries, 3 as null and 1 with the correct value. like this: [{"rating":null},{"rating":null},{"rating":5},{"rating":null}] Using first isn't suppose to retreive only the exact correspondent result?

What is the best way to achieve this? the idea is to have a table like the following:

DAY. Rating 1 2 3 5 4 ...

1 like
vincent15000's avatar

@musqsim ->first() is intended to retrieve the first line in the table which corresponds to the ->where() clause.

musqsim's avatar

So, I can't find a better solution, so I went with this and works well:

        $user = Patient::where('id', $request->id)->with('clinic_session')->with('financial_movement')->first();
    
        $a = 1;
        $end = $this->getDaysInMonth($current_month);
        $data = new Collection();
        while ($a <=  $end) {
                    $date = new Carbon(date('Y').date('m').$a);
                    $financial = $user->financial_movement->where('date', $date->toDateString())->first() ? $user->financial_movement->where('date', $date->toDateString())->first() : null;
                    $data->push((object)['day' => $a,
                                            'session'=> $user->clinic_session->where('date', $date->toDateString())->first() ? $user->clinic_session->where('date', $date->toDateString())->first()->date_start : null,
                                            'payment'=> $financial ? $financial->value : null,
                                            'method'=> $financial ? $financial->method : null,
                                            'notes'=> $financial ? $financial->notes : null,

                    ]);
                    $a++;
                }
  
        
        return view('backend.financials.show')->with('user', $user)->with('data', $data);
1 like

Please or to participate in this conversation.