Created
October 23, 2019 14:04
-
-
Save nullthoughts/babcf06f4f06b6718e4c2e1c4a734be1 to your computer and use it in GitHub Desktop.
scopeBySaleType
This file contains hidden or 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
public function scopeBySaleType($query, type) | |
{ | |
$query->whereHas('sales', function ($query) use ($type) { | |
$query->where('id', function ($sub) { | |
$sub->from('sales') | |
->selectRaw('max(id)') | |
->whereColumn('sales.inventory_id', 'inventories.id'); | |
})->where('type', $type); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Macro to limit results by a sub query value:
Using the macro:
If you'd like to get the latest sale as a dynamic relationship as well: