/** * @param QueryBuilder $builder * @param $data * * @return void */ public function applyFilterConstraint(QueryBuilder $builder, $data) { if (empty($data)) { return; } $builder->whereIn($this->relation->getForeignKey(), explode(',', $data)); }
protected function applyBelongsTo(Query $query, Model $model, BelongsTo $belongsTo, $name) { if (isset($this->joinClasses[$name])) { return; } $modelTable = ''; // If the table has already an alias if (str_contains($name, '.')) { $path = explode('.', $name); array_pop($path); $parentPath = implode('.', $path); if (isset($this->joinAliases[$parentPath])) { $modelTable = $this->joinAliases[$parentPath]; } } if (!$modelTable) { $modelTable = $belongsTo->getParent()->getTable(); } $related = $belongsTo->getRelated(); $relatedTable = $related->getTable(); $foreignKey = $belongsTo->getForeignKey(); $otherKey = $belongsTo->getOtherKey(); $alias = $this->joinNameToAlias($name); $joinMethod = $this->getJoinMethod($name); $query->{$joinMethod}("{$relatedTable} AS {$alias}", "{$modelTable}.{$foreignKey}", '=', "{$alias}.{$otherKey}"); $query->distinct(); $this->addQueryColumn("{$modelTable}.{$foreignKey}"); $this->joinClasses[$name] = $belongsTo->getRelated(); $this->joinTable[$name] = $relatedTable; $this->joinAliases[$name] = $alias; }
/** * Makes a raw where condition string from a BelongsTo relation instance. * * @param BelongsTo $relation * @return string */ protected function getConditionStringFromBelongsTo(BelongsTo $relation) { $id = $this->getAttribute($relation->getForeignKey()); // an empty foreign key will, as a null-scope, remove the item from any list if (null === $id) { return null; } return '`' . $relation->getForeignKey() . '` = ' . (int) $id; }