1

I'm new for laravel I tried some eloquent collections but not get required result help me to solve. I have data like this TABLE NAME (attendance):

id  |     name      |student_id   |  date_of_attendance |   attendance |
1   |    john       |    STD_01   |     2024-01-01      |       P      |
2   |    john       |    STD_01   |     2024-01-02      |       P      |
3   |    sean       |    STD_02   |     2024-01-01      |       P      |
4   |    sean       |    STD_02   |     2024-01-02      |       A      |

How to merge it to become like this? I want to show only one rows of each student in the views.

enter image description here

livewire controller code

$this->StudenAttendance = Attendance::get();

1 Answer 1

1

I think the simplest approach would be to use the Eloquent Collection's groupBy method.

Your call to the model to get attendance returns an Eloquent Collection

$this->StudenAttendance = Attendance::get();

You can simply chain on groupBy('student_id') which will turn your student attendance collection into a collection for each student. Then you can create your table rows by looping through each student and putting the attendance for each date into your table.

See docs: https://laravel.com/docs/10.x/collections#method-groupby

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

3 Comments

can you post your answer what you telling for better understanding
I Googled this same problem, thay used left join, I tried to integrate with my code but I failed. If you have any idea using left joint please share code, and which one better according you groupBy or left join
You could do the same with a join but the groupBy method on the collection would be simpler. The documentation tells you how to apply the groupBy method, if you need to see what the result looks like, you can make use of the dump() and dd() methods

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.