Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Created January 17, 2019 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarektkaczyk/cc8ad3a71f14428204efd54d9a99a510 to your computer and use it in GitHub Desktop.
Save jarektkaczyk/cc8ad3a71f14428204efd54d9a99a510 to your computer and use it in GitHub Desktop.
first/last/min/max on related table in Laravel Builder/Eloquent
<?php
$this->builder
->select('*') // needs to be here, otherwise the only returned column will be `last_login`
->selectSub(function ($q) {
$q->from('action_logs')
->whereColumn('user_id', 'users.id')
->selectRaw('max(created_at)');
}, 'last_login')
->get();
// all columns from the table...
// last_login => '2019-01-16 12:34:56'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment