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

noblemfd's avatar

How to know those that have logged-in or not logged-in within a set date

I have this code in Laravel-5.8 to filter the users within a set date as shown below:

public function userReport(Request $request)
{
    $userCompany = Auth::user()->company_id;

    $users= User::where('hr_status', 0)->where('company_id', $userCompany)->orderBy('last_login_at', 'desc');
    
    $start_date = $request->start_date;
    $end_date = $request->end_date; 
    
    $render=[];
    
    if(isset($request->start_date) && isset($request->end_date))
    {
        $users=$users->whereBetween('last_login_at',[$start_date.' 00:00:00',$end_date.' 23:59:59']);
        $render['start_date']=$request->start_date;
        $render['end_date']=$request->end_date;
    }elseif(isset($request->start_date))
    {
        $users=$users->where('last_login_at',$request->start_date);
        $userr['start_date']=$request->start_date;
    }
   
    $users= $users->paginate(15);
    $users= $users->appends($render);
    $data['users'] = $users;
     return view('report.userReport',$data);
} 

The date for reference point is last_login_date

View

<div class="row" style="margin-bottom: 10px">
    {{ Form::model(request(),['method'=>'get']) }}
    <div class="col-sm-3">
        {{ Form::date('start_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
    </div>
    <div class="col-sm-3">
        {{ Form::date('end_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
    </div>          
    <div class="col-xs-2">
        {{ Form::submit('Search',['class'=>'btn btn-warning']) }}
         </div>
    {{ Form::close() }}
</div>

How do I complete the code in the controller to know the:

  1. users that have logged- withing the specified date

  2. users that have not logged-in within specified date

Thanks

0 likes
1 reply
bobbybouwmann's avatar
Level 88

Well, the idea is that you first fetch all users that it within the the first query. Then you can simply fetch all users that you didn't fetch yet

$loggedInUsers = User::whereBetween('last_login_date, [$startDate, $endDate])->get();

// Get all the users, except the ones you already queried
$notLoggedInUsers = User::whereNotIn('id', $loggedInUsers->pluck('id'))->get();

Please or to participate in this conversation.