/** * Join the query with a subquery. Warning, in order to use this method * properly, the join must be executed at the start of the query. If * it's added t the end of the query, the bindings won't match up. * * @param \October\Rain\Database\Builder $query Query being joined to * @param \October\Rain\Database\Builder $subquery Subquery being joined * @param string $alias Joined table alias * @param string $left Left side of condition * @param string $operator Join condition operator * @param string $right Right side of condition * @param string $join Join type [ join, leftJoin ] * @return \October\Rain\Database\Builder */ public function scopeJoinSubquery($query, $subquery, $alias, $left, $operator, $right, $join = 'join') { $self = $this->getTable() . '.*'; if (!in_array($self, $query->getQuery()->columns)) { $query->addSelect($self); } $subquery = $subquery->getQuery(); $raw = DB::raw('(' . $subquery->toSql() . ') ' . $alias); return $query->{$join}($raw, $left, $operator, $right)->mergeBindings($subquery); }
/** * Left joins a subquery containing the product price * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeJoinPrice($query) { $alias = 'prices'; $grammar = $query->getQuery()->getGrammar(); $subquery = Price::isRunning()->addselect('bedard_shop_prices.product_id')->selectRaw('MIN(' . $grammar->wrap('bedard_shop_prices.price') . ') as ' . $grammar->wrap('price'))->groupBy('bedard_shop_prices.product_id'); return $query->addSelect($alias . '.price')->joinSubquery($subquery, $alias, 'bedard_shop_products.id', '=', $alias . '.product_id'); }