Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 891 Bytes

easy-subqueries-select-sub.md

File metadata and controls

35 lines (30 loc) · 891 Bytes

Subqueries made easy with selectSub

Subqueries allow us to select extra columns (attributes) right in our primary database query.

$lastLogin = Login::select('created_at')
    ->whereColumn('user_id', 'users.id')
    ->latest()
    ->limit(1)
    ->getQuery();

$users = User::select('users.*')
    ->selectSub($lastLogin, 'last_login_at')
    ->get();
@foreach ($users as $user)
    <tr>
        <td>{{ $user->name }}</td>
        <td>{{ $user->email }}</td>
        <td>
            @if ($user->last_login_at)
                {{ $user->last_login_at->format('M j, Y \a\t g:i a') }}
            @else
                Never
            @endif
        </td>
    </tr>
@endforeach
Added by Found via
Marco Mark Jonathan Reinink