Created
January 17, 2019 09:05
-
-
Save jarektkaczyk/cc8ad3a71f14428204efd54d9a99a510 to your computer and use it in GitHub Desktop.
first/last/min/max on related table in Laravel Builder/Eloquent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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