title: Laravel: How would you Query this Table and group two rows with the sameby their staff ID
body: <p>I wish to achieve such a table:
  <a href="https://i.stack.imgur.com/tacXR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tacXR.png" alt="Intended Table" /></a></p>
  <p>I currently have this table:
  <a href="https://i.stack.imgur.com/o1Rzi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o1Rzi.png" alt="Current Table" /></a></p>
  <p>EDIT!!</p>
  <p>This is my attendance table in My sql.<a href="https://i.stack.imgur.com/fj7RT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fj7RT.png" alt="enter image description here" /></a></p>
  <p>This is the attendance table in mysql:
  <a href="https://i.stack.imgur.com/Z4tq4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z4tq4.png" alt="Attendance Table in My Sql" /></a></p>
  <p>This is the Staff Table in mySql</p>
  <p><a href="https://i.stack.imgur.com/8LRWH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8LRWH.png" alt="Staff Table" /></a></p>
  <p>Dashboard Controller</p>
  <pre><code> public function index()
      {
             $data = [
             'attendances'=&gt;Attendance::query()
                 -&gt;where('type','!=','255')
                  -&gt;with('staff')-&gt;get()
          ];
  
  
          return  view('dashboard.attendance_table',$data);
  
  
  //        $attendances = Attendance::all(); // or whatever Eloquent query you want that returns a collection
  //
  //        $grouped = $attendances-&gt;groupBy('staff_id');
  //
  //        dd($grouped);
      }
  </code></pre>
  <p>attendance.blade</p>
  <pre><code> &lt;div class=&quot;row&quot;&gt;
                      &lt;div class=&quot;col-lg-12&quot;&gt;
                          &lt;div class=&quot;card overflow-hidden&quot;&gt;
                              &lt;div class=&quot;card-body&quot;&gt;
                                  &lt;div&gt;
                                      &lt;h6 class=&quot;main-content-label mb-1&quot;&gt;Attendance Table&lt;/h6&gt;
  
                                  &lt;/div&gt;
                                  &lt;div class=&quot;table-responsive&quot;&gt;
                                      &lt;table id=&quot;exportexampl&quot; class=&quot;table table-bordered border-t0 key-buttons text-nowrap w-100 display &quot; &gt;
                                          &lt;thead&gt;
                                          &lt;tr&gt;
                                              &lt;th&gt;#&lt;/th&gt;
                                              &lt;th&gt;Staff Name&lt;/th&gt;
                                              &lt;th&gt;User ID&lt;/th&gt;
                                              &lt;th&gt;Time In/ Time Out&lt;/th&gt;
                                              &lt;th&gt;Card No.&lt;/th&gt;
                                              &lt;th&gt;Date&lt;/th&gt;
                                              &lt;th&gt;Time&lt;/th&gt;
  
                                          &lt;/tr&gt;
                                          &lt;/thead&gt;
                                          &lt;tbody&gt;
                                          @foreach($attendances as $attendance)
                                              &lt;tr&gt;
                                                  &lt;td&gt;{{$loop-&gt;iteration}}&lt;/td&gt;
                                                  &lt;td&gt;{{ $attendance-&gt;staff-&gt;name }}&lt;/td&gt;
                                                  &lt;td&gt;{{ $attendance-&gt;staff-&gt;userid }}&lt;/td&gt;
                                                  {{--                                          &lt;td&gt;{{gettype($attendance-&gt;type)}}&lt;/td&gt;--}}
                                                  &lt;td&gt;{{ ($attendance-&gt;type==0) ? 'Time in': 'Time out'}}&lt;/td&gt;
                                                  &lt;td&gt;{{ $attendance-&gt;staff-&gt;cardno }}&lt;/td&gt;
                                                  &lt;td&gt;{{ \Carbon\Carbon::parse($attendance-&gt;timestamp)-&gt;toDateString() }}&lt;/td&gt;
                                                  &lt;td&gt;{{ \Carbon\Carbon::parse($attendance-&gt;timestamp)-&gt;format(&quot;H:i:s&quot;) }}&lt;/td&gt;
                                              &lt;/tr&gt;
                                          @endforeach
                                          &lt;/tbody&gt;
                                      &lt;/table&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  
  </code></pre>
  <p>How would you group the current attendance table so as to achieve my desired attendance table as shown above.
  Note:
  Time in is 'type'=0
  time out is type =1</p>