/** * 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); }
/** * This exists to makes statuses sortable by assigning them a value * * Expired -1 * Running 0 * Upcoming 1 * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeSelectStatus($query) { $grammar = $query->getQuery()->getGrammar(); $start_at = $grammar->wrap($this->table . '.start_at'); $end_at = $grammar->wrap($this->table . '.end_at'); $now = Carbon::now(); $subquery = "CASE " . "WHEN ({$end_at} IS NOT NULL AND {$end_at} < '{$now}') THEN -1 " . "WHEN ({$start_at} IS NOT NULL AND {$start_at} > '{$now}') THEN 1 " . "ELSE 0 " . "END"; return $query->selectSubquery($subquery, 'status'); }
/** * Get the underlying query builder instance. * * @return \Illuminate\Database\Query\Builder|static * @static */ public static function getQuery() { //Method inherited from \Illuminate\Database\Eloquent\Builder return \October\Rain\Database\Builder::getQuery(); }
/** * This exists to makes statuses sortable by assigning them a value * * Disabled -2 * Out of stock -1 * Normal 0 * Discounted 1 * * @param \October\Rain\Database\Builder $query * @return \October\Rain\Database\Builder */ public function scopeSelectStatus($query) { $grammar = $query->getQuery()->getGrammar(); $price = $grammar->wrap('price'); $inventory = $grammar->wrap('inventory'); $is_enabled = $grammar->wrap('bedard_shop_products.is_enabled'); $base_price = $grammar->wrap('bedard_shop_products.base_price'); $subquery = "CASE " . "WHEN {$is_enabled} = 0 THEN -2 " . "WHEN ({$inventory} IS NULL or {$inventory} = 0) THEN -1 " . "WHEN {$price} < {$base_price} THEN 1 " . "ELSE 0 " . "END"; return $query->selectSubquery($subquery, 'status'); }