Skip to content

Get users current/latest status #129

@judaemon

Description

@judaemon

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;
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions