Skip to content

Instantly share code, notes, and snippets.

@amrikarisma
Created August 24, 2022 18:04
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 amrikarisma/be74aa0e81aebab08570cda73e02a865 to your computer and use it in GitHub Desktop.
Save amrikarisma/be74aa0e81aebab08570cda73e02a865 to your computer and use it in GitHub Desktop.
Example scope with lowest price
<?php
public function scopeWithPrice($smartphone)
{
$queryGetMinPrice = $smartphone->select([
'*',
'price' => ProductPrice::select('price')->where('priceable_type', Smartphone::class)->whereColumn('priceable_id', 'smartphones.id')->orderBy('price', 'ASC')->take(1)
]);
return $queryGetMinPrice;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment