laravel-model-status icon indicating copy to clipboard operation
laravel-model-status copied to clipboard

Get users current/latest status

Open judaemon opened this issue 1 year ago • 0 comments

Is there any way to do this?

$users = User::currentStatus()->get(); $users = User::latestStatus()->get();

or other ways to join the status table and get the latest status. I have this in my model idk if its good enough or there are other way.

public function scopeWithLatestStatuses($query)
  {
    $latestStatuses = DB::table('statuses')
      ->select('model_id', DB::raw('MAX(id) as latest_status_id'))
      ->where('model_type', UserModel::class)
      ->groupBy('model_id');

    $query->leftJoinSub(
      $latestStatuses,
      'latest_statuses',
      function ($join) {
        $join->on('users.id', '=', 'latest_statuses.model_id');
      }
    )->leftJoin('statuses', 'statuses.id', '=', 'latest_statuses.latest_status_id');

    return $query;
  }

judaemon avatar Oct 09 '24 03:10 judaemon