Selle
Selle

Reputation: 37

How to get the first data and last data from the same data in the database?

I want to output the time in and time out from this database. enter image description here

So i want to get the first data and last data in date 2017-06-13 and 2017-06-15. How can i do that using Laravel 5.3?

Upvotes: 0

Views: 195

Answers (3)

Sergii K
Sergii K

Reputation: 866

May be this will work for you:

DB::table('emp')
  ->select(DB::raw("MIN(`time`) AS `time_in`, MAX(`time`) AS `time_out`"))
  ->where('date', '2017-06-13')
  ->get();

Output:

{
    time_in: "19:38:33",
    time_out: "22:14:10"
}

Upvotes: 1

LuFFy
LuFFy

Reputation: 9307

Try Below Code :

$attendancehours = DB::select(
    DB::RAW('TIMESTAMPDIFF(HOUR,CONCAT(in_date," ",in_time),CONCAT(out_date," ",out_time)'))->
    table('staff_attendances')->
    whereBetween('in_date', array($date1, $date2))->
    where('id', $sID)
    ->get();

Upvotes: 0

user320487
user320487

Reputation:

You can order your table by the date and time columns and then use Eloquent to take ->first() and ->last() using those methods.

$collection = BiometricsAttendance::where('emp_id', $emp)->whereBet‌​ween('date', [$start, $end])->groupBy('date')->orderBy('time', 'asc');

$timein = $collection->first();

$timeout = $collection->last();

Upvotes: 0

Related Questions