plamen911

Laravel eloquent query

May 22nd, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. $variant = \App\ProductVariant::select('id', 'option_ids', 'price')->whereJsonContains('option_ids', $request->input('options'))->with([
  2.             'stock' => function($query) {
  3.                 $query->select('id', 'variant_id', 'quantity');
  4.             },
  5.             'discount' => function($query) {
  6.                 /** @var \Illuminate\Database\Query\Builder $query */
  7.                 $query->select('variant_id', 'percents')
  8.                     ->selectRaw('ROUND(product_variants.price * percents / 100, 2) AS price_row')
  9.                     ->leftJoin('product_variants', 'product_variants.id', '=', 'discounts.variant_id')
  10.                     ->where('expiration', '>', Carbon::now());
  11.             }
  12.         ])->first();
Advertisement
Add Comment
Please, Sign In to add comment