Phase 0: Fundament für Inhaus
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s

- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3)
- Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung
- Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü
- Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person
- Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“
- Benutzerverwaltung im Admin, Zugang vorläufig über is_admin
- vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/
- PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung
- VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-25 20:35:30 +00:00
parent 7197eaebf2
commit 9c0f8de791
15771 changed files with 1564103 additions and 5 deletions

View file

@ -0,0 +1,26 @@
<?php
namespace Kirschbaum\PowerJoins;
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Kirschbaum\PowerJoins\Mixins\JoinRelationship;
use Kirschbaum\PowerJoins\Mixins\QueryBuilderExtraMethods;
use Kirschbaum\PowerJoins\Mixins\QueryRelationshipExistence;
use Kirschbaum\PowerJoins\Mixins\RelationshipsExtraMethods;
class EloquentJoins
{
/**
* Register macros with Eloquent.
*/
public static function registerEloquentMacros()
{
EloquentQueryBuilder::mixin(new JoinRelationship());
EloquentQueryBuilder::mixin(new QueryRelationshipExistence());
QueryBuilder::mixin(new QueryBuilderExtraMethods());
Relation::mixin(new RelationshipsExtraMethods());
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Kirschbaum\PowerJoins;
/**
* @method static as(string $alias)
*/
class FakeJoinCallback extends PowerJoinClause
{
protected ?string $joinType = null;
public function getAlias(): ?string
{
return $this->alias;
}
public function getJoinType(): ?string
{
return $this->joinType;
}
public function __call($name, $arguments)
{
if ($name === 'as') {
$this->alias = $arguments[0];
} elseif ($name === 'joinType') {
$this->joinType = $arguments[0];
} elseif ($name === 'left') {
$this->joinType = 'leftPowerJoin';
} elseif ($name === 'right') {
$this->joinType = 'rightPowerJoin';
} elseif ($name === 'inner') {
$this->joinType = 'powerJoin';
}
return $this;
}
}

View file

@ -0,0 +1,259 @@
<?php
namespace Kirschbaum\PowerJoins;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough;
use Illuminate\Database\Eloquent\Relations\Relation;
use WeakMap;
class JoinsHelper
{
public static WeakMap $instances;
/**
* Cache to determine which query model belongs to which query.
* This is used to determine if a query is a clone of another
* query and therefore if we should refresh the model in it.
*
* The keys are the model objects, and the value is the spl
* object ID of the associated Eloquent builder instance.
*/
public static WeakMap $modelQueryDictionary;
/**
* An array of `beforeQuery` callbacks that
* are registered by the library.
*/
public static WeakMap $beforeQueryCallbacks;
protected function __construct()
{
static::$instances ??= new WeakMap();
static::$modelQueryDictionary ??= new WeakMap();
static::$beforeQueryCallbacks ??= new WeakMap();
$this->joinRelationshipCache = new WeakMap();
}
public static function make($model): static
{
static::$instances ??= new WeakMap();
return static::$instances[$model] ??= new self();
}
/**
* Cache to not join the same relationship twice.
*/
private WeakMap $joinRelationshipCache;
/**
* Join method map.
*/
public static $joinMethodsMap = [
'join' => 'powerJoin',
'leftJoin' => 'leftPowerJoin',
'rightJoin' => 'rightPowerJoin',
];
/**
* Ensure that any query model can only belong to
* maximum one query, e.g. because of cloning.
*/
public static function ensureModelIsUniqueToQuery($query): void
{
$originalModel = $query->getModel();
$querySplObjectId = spl_object_id($query);
if (
isset(static::$modelQueryDictionary[$originalModel])
&& static::$modelQueryDictionary[$originalModel] !== $querySplObjectId
) {
// If the model is already associated with another query, we need to clone the model.
// This can happen if a certain query, *before having interacted with the library
// `joinRelationship()` method*, was cloned by previous code.
// Preserve the from clause (including any alias) before setModel overwrites it
$originalFrom = $query->getQuery()->from;
$query->setModel($model = new ($query->getModel()));
$model->mergeCasts($originalModel->getCasts());
if ($originalFrom) {
$query->getQuery()->from = $originalFrom;
}
// Link the Spl Object ID of the query to the new model...
static::$modelQueryDictionary[$model] = $querySplObjectId;
// If there is a `JoinsHelper` with a cache associated with the old model,
// we will copy the cache over to the new fresh model clone added to it.
$originalJoinsHelper = JoinsHelper::make($originalModel);
$joinsHelper = JoinsHelper::make($model);
foreach ($originalJoinsHelper->joinRelationshipCache[$originalModel] ?? [] as $relation => $value) {
$joinsHelper->markRelationshipAsAlreadyJoined($model, $relation);
}
} else {
static::$modelQueryDictionary[$originalModel] = $querySplObjectId;
}
$query->onClone(static function (Builder $query) {
$originalModel = $query->getModel();
$originalJoinsHelper = JoinsHelper::make($originalModel);
// Preserve the from clause (including any alias) before setModel overwrites it
$originalFrom = $query->getQuery()->from;
// Ensure the model of the cloned query is unique to the query.
$query->setModel($model = new $originalModel());
$model->mergeCasts($originalModel->getCasts());
// Restore the original from clause if it was set
if ($originalFrom) {
$query->getQuery()->from = $originalFrom;
}
// Update any `beforeQueryCallbacks` to link to the new `$this` as Eloquent Query,
// otherwise the reference to the current Eloquent query goes wrong. These query
// callbacks are stored on the `QueryBuilder` instance and therefore do not get
// an instance of Eloquent Builder passed, but an instance of `QueryBuilder`.
foreach ($query->getQuery()->beforeQueryCallbacks as $key => $beforeQueryCallback) {
/** @var Closure $beforeQueryCallback */
if (isset(static::$beforeQueryCallbacks[$beforeQueryCallback])) {
static::$beforeQueryCallbacks[$query->getQuery()->beforeQueryCallbacks[$key] = $beforeQueryCallback->bindTo($query)] = true;
}
}
$joinsHelper = JoinsHelper::make($model);
foreach ($originalJoinsHelper->joinRelationshipCache[$originalModel] ?? [] as $relation => $value) {
$joinsHelper->markRelationshipAsAlreadyJoined($model, $relation);
}
});
}
public static function clearCacheBeforeQuery($query): void
{
$beforeQueryCallback = function () {
/* @var Builder $this */
JoinsHelper::make($this->getModel())->clear();
};
$query->getQuery()->beforeQuery(
$beforeQueryCallback = $beforeQueryCallback->bindTo($query)
);
static::$beforeQueryCallbacks[$beforeQueryCallback] = true;
}
/**
* Format the join callback.
*/
public function formatJoinCallback($callback)
{
if (is_string($callback)) {
return function ($join) use ($callback) {
$join->as($callback);
};
}
return $callback;
}
public function generateAliasForRelationship(Relation $relation, string $relationName): array|string
{
if ($relation instanceof BelongsToMany || $relation instanceof HasManyThrough) {
return [
md5($relationName.'table1'.uniqid('', true)),
md5($relationName.'table2'.uniqid('', true)),
];
}
return md5($relationName.uniqid('', true));
}
/**
* Get the join alias name from all the different options.
*/
public function getAliasName(bool $useAlias, Relation $relation, string $relationName, string $tableName, $callback): string|array|null
{
if ($callback) {
if (is_callable($callback)) {
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
$callback($fakeJoinCallback);
if ($fakeJoinCallback->getAlias()) {
return $fakeJoinCallback->getAlias();
}
}
if (is_array($callback) && $relation instanceof HasOneOrManyThrough) {
$alias = [null, null];
$throughParentTable = $relation->getThroughParent()->getTable();
if (isset($callback[$throughParentTable])) {
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $throughParentTable);
$callback[$throughParentTable]($fakeJoinCallback);
if ($fakeJoinCallback->getAlias()) {
$alias[0] = $fakeJoinCallback->getAlias();
}
}
$relatedTable = $relation->getRelated()->getTable();
if (isset($callback[$relatedTable])) {
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $relatedTable);
$callback[$relatedTable]($fakeJoinCallback);
if ($fakeJoinCallback->getAlias()) {
$alias[1] = $fakeJoinCallback->getAlias();
}
}
return $alias;
}
if (is_array($callback) && isset($callback[$tableName])) {
$fakeJoinCallback = new FakeJoinCallback($relation->getBaseQuery(), 'inner', $tableName);
$callback[$tableName]($fakeJoinCallback);
if ($fakeJoinCallback->getAlias()) {
return $fakeJoinCallback->getAlias();
}
}
}
return $useAlias
? $this->generateAliasForRelationship($relation, $relationName)
: null;
}
/**
* Checks if the relationship was already joined.
*/
public function relationshipAlreadyJoined($model, string $relation): bool
{
return isset($this->joinRelationshipCache[$model][$relation]);
}
/**
* Marks the relationship as already joined.
*/
public function markRelationshipAsAlreadyJoined($model, string $relation): void
{
$this->joinRelationshipCache[$model] ??= [];
$this->joinRelationshipCache[$model][$relation] = true;
}
public function clear(): void
{
$this->joinRelationshipCache = new WeakMap();
}
}

View file

@ -0,0 +1,615 @@
<?php
namespace Kirschbaum\PowerJoins\Mixins;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\JoinsHelper;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\StaticCache;
/**
* @mixin Builder
*
* @method \Illuminate\Database\Eloquent\Model getModel()
*
* @property Builder $query
*/
class JoinRelationship
{
/**
* New clause for making joins, where we pass the model to the joiner class.
*/
public function powerJoin(): Closure
{
return function ($table, $first, $operator = null, $second = null, $type = 'inner', $where = false): static {
$model = $operator instanceof Model ? $operator : null;
$join = $this->newPowerJoinClause($this->query, $type, $table, $model);
// If the first "column" of the join is really a Closure instance the developer
// is trying to build a join with a complex "on" clause containing more than
// one condition, so we'll add the join and call a Closure with the query.
if ($first instanceof Closure) {
$first($join);
$this->query->joins[] = $join;
$this->query->addBinding($join->getBindings(), 'join');
}
// If the column is simply a string, we can assume the join simply has a basic
// "on" clause with a single condition. So we will just build the join with
// this simple join clauses attached to it. There is not a join callback.
else {
$method = $where ? 'where' : 'on';
$this->query->joins[] = $join->$method($first, $operator, $second);
$this->query->addBinding($join->getBindings(), 'join');
}
return $this;
};
}
/**
* New clause for making joins, where we pass the model to the joiner class.
*/
public function leftPowerJoin(): Closure
{
return function ($table, $first, $operator = null, $second = null) {
return $this->powerJoin($table, $first, $operator, $second, 'left');
};
}
/**
* New clause for making joins, where we pass the model to the joiner class.
*/
public function rightPowerJoin(): Closure
{
return function ($table, $first, $operator = null, $second = null) {
return $this->powerJoin($table, $first, $operator, $second, 'right');
};
}
public function newPowerJoinClause(): Closure
{
return function (QueryBuilder $parentQuery, string $type, string $table, ?Model $model = null) {
return new PowerJoinClause($parentQuery, $type, $table, $model);
};
}
/**
* Join the relationship(s).
*/
public function joinRelationship(): Closure
{
return function (
string $relationName,
Closure|array|string|null $callback = null,
string $joinType = 'join',
bool $useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null,
) {
$joinType = JoinsHelper::$joinMethodsMap[$joinType] ?? $joinType;
$useAlias = is_string($callback) ? false : $useAlias;
$joinHelper = JoinsHelper::make($this->getModel());
$callback = $joinHelper->formatJoinCallback($callback);
JoinsHelper::ensureModelIsUniqueToQuery($this);
JoinsHelper::clearCacheBeforeQuery($this);
// Check if the main table has an alias (e.g., "posts as p") and set it as the main table or alias if it does.
$fromClause = $this->getQuery()->from;
$mainTableOrAlias = $this->getModel()->getTable();
if ($fromClause && is_string($fromClause) && preg_match('/^.+\s+as\s+["\'\`]?(.+?)["\'\`]?$/i', $fromClause, $matches)) {
// Register the alias for the main model so joins use it
$mainTableOrAlias = $matches[1];
StaticCache::setTableAliasForModel($this->getModel(), $mainTableOrAlias);
}
if (is_null($this->getSelect())) {
$defaultSelect = sprintf('%s.*', $mainTableOrAlias);
$this->getQuery()->beforeQuery(function ($queryBuilder) use ($defaultSelect) {
if (is_null($queryBuilder->columns) || $queryBuilder->columns === ['*']) {
$queryBuilder->columns = [$defaultSelect];
}
});
}
if (Str::contains($relationName, '.')) {
$this->joinNestedRelationship($relationName, $callback, $joinType, $useAlias, $disableExtraConditions, $morphable);
return $this;
}
$relationCallback = $callback;
if ($callback && is_array($callback) && isset($callback[$relationName]) && is_array($callback[$relationName])) {
$relationCallback = $callback[$relationName];
}
$relation = $this->getModel()->{$relationName}();
$relationQuery = $relation->getQuery();
$alias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relationQuery->getModel()->getTable(),
$relationCallback
);
if ($relation instanceof BelongsToMany && !is_array($alias)) {
$extraAlias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getTable(),
$relationCallback
);
$alias = [$extraAlias, $alias];
}
$aliasString = is_array($alias) ? implode('.', $alias) : $alias;
$useAlias = $alias ? true : $useAlias;
$relationJoinCache = $alias
? "{$aliasString}.{$relationQuery->getModel()->getTable()}.{$relationName}"
: "{$relationQuery->getModel()->getTable()}.{$relationName}";
if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
return $this;
}
if ($useAlias) {
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}
$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
$relation->performJoinForEloquentPowerJoins(
builder: $this,
joinType: $joinType,
callback: $relationCallback,
alias: $alias,
disableExtraConditions: $disableExtraConditions,
morphable: $morphable,
);
// Clear only the related model's alias from cache after join is performed
if ($useAlias) {
unset(StaticCache::$powerJoinAliasesCache[spl_object_id($relation->getModel())]);
}
return $this;
};
}
/**
* Join the relationship(s) using table aliases.
*/
public function joinRelationshipUsingAlias(): Closure
{
return function (string $relationName, Closure|array|string|null $callback = null, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relationName, $callback, 'join', true, $disableExtraConditions, morphable: $morphable);
};
}
/**
* Left join the relationship(s) using table aliases.
*/
public function leftJoinRelationshipUsingAlias(): Closure
{
return function (string $relationName, Closure|array|string|null $callback = null, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relationName, $callback, 'leftJoin', true, $disableExtraConditions, morphable: $morphable);
};
}
/**
* Right join the relationship(s) using table aliases.
*/
public function rightJoinRelationshipUsingAlias(): Closure
{
return function (string $relationName, Closure|array|string|null $callback = null, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relationName, $callback, 'rightJoin', true, $disableExtraConditions, morphable: $morphable);
};
}
public function joinRelation(): Closure
{
return function (
string $relationName,
Closure|array|string|null $callback = null,
string $joinType = 'join',
bool $useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null,
) {
return $this->joinRelationship($relationName, $callback, $joinType, $useAlias, $disableExtraConditions, morphable: $morphable);
};
}
public function leftJoinRelationship(): Closure
{
return function (string $relationName, Closure|array|string|null $callback = null, bool $useAlias = false, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relationName, $callback, 'leftJoin', $useAlias, $disableExtraConditions, morphable: $morphable);
};
}
public function leftJoinRelation(): Closure
{
return function (string $relation, Closure|array|string|null $callback = null, bool $useAlias = false, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relation, $callback, 'leftJoin', $useAlias, $disableExtraConditions, morphable: $morphable);
};
}
public function rightJoinRelationship(): Closure
{
return function (string $relation, Closure|array|string|null $callback = null, bool $useAlias = false, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions, morphable: $morphable);
};
}
public function rightJoinRelation(): Closure
{
return function (string $relation, Closure|array|string|null $callback = null, bool $useAlias = false, bool $disableExtraConditions = false, ?string $morphable = null) {
return $this->joinRelationship($relation, $callback, 'rightJoin', $useAlias, $disableExtraConditions, morphable: $morphable);
};
}
/**
* Join nested relationships.
*/
public function joinNestedRelationship(): Closure
{
return function (
string $relationships,
Closure|array|string|null $callback = null,
string $joinType = 'join',
bool $useAlias = false,
bool $disableExtraConditions = false,
?string $morphable = null,
) {
$relations = explode('.', $relationships);
$joinHelper = JoinsHelper::make($this->getModel());
/** @var Relation */
$latestRelation = null;
$part = [];
foreach ($relations as $relationName) {
$part[] = $relationName;
$fullRelationName = join('.', $part);
$currentModel = $latestRelation ? $latestRelation->getModel() : $this->getModel();
$relation = $currentModel->{$relationName}();
$relationCallback = null;
if ($callback && is_array($callback) && isset($callback[$relationName])) {
$relationCallback = $callback[$relationName];
}
if ($callback && is_array($callback) && isset($callback[$fullRelationName])) {
$relationCallback = $callback[$fullRelationName];
}
$alias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getQuery()->getModel()->getTable(),
$relationCallback
);
if ($alias && $relation instanceof BelongsToMany && !is_array($alias)) {
$extraAlias = $joinHelper->getAliasName(
$useAlias,
$relation,
$relationName,
$relation->getTable(),
$relationCallback
);
$alias = [$extraAlias, $alias];
}
$aliasString = is_array($alias) ? implode('.', $alias) : $alias;
$useAlias = $alias ? true : $useAlias;
if ($alias) {
$relationJoinCache = $latestRelation
? "{$aliasString}.{$relation->getQuery()->getModel()->getTable()}.{$latestRelation->getModel()->getTable()}.{$relationName}"
: "{$aliasString}.{$relation->getQuery()->getModel()->getTable()}.{$relationName}";
} else {
$relationJoinCache = $latestRelation
? "{$relation->getQuery()->getModel()->getTable()}.{$latestRelation->getModel()->getTable()}.{$relationName}"
: "{$relation->getQuery()->getModel()->getTable()}.{$relationName}";
}
if ($useAlias) {
StaticCache::setTableAliasForModel($relation->getModel(), $alias);
}
if ($joinHelper->relationshipAlreadyJoined($this->getModel(), $relationJoinCache)) {
$latestRelation = $relation;
continue;
}
$relation->performJoinForEloquentPowerJoins(
$this,
$joinType,
$relationCallback,
$alias,
$disableExtraConditions,
$morphable
);
$latestRelation = $relation;
$joinHelper->markRelationshipAsAlreadyJoined($this->getModel(), $relationJoinCache);
}
StaticCache::clear();
return $this;
};
}
/**
* Order by a field in the defined relationship.
*/
public function orderByPowerJoins(): Closure
{
return function (string|array $sort, string $direction = 'asc', ?string $aggregation = null, string $joinType = 'join', $aliases = null) {
if (is_array($sort)) {
$relationships = explode('.', $sort[0]);
$column = $sort[1];
$latestRelationshipName = $relationships[count($relationships) - 1];
} else {
$relationships = explode('.', $sort);
$column = array_pop($relationships);
$latestRelationshipName = $relationships[count($relationships) - 1];
}
$this->joinRelationship(relationName: implode('.', $relationships), callback: $aliases, joinType: $joinType);
$latestRelationshipModel = array_reduce($relationships, function ($model, $relationshipName) {
return $model->$relationshipName()->getModel();
}, $this->getModel());
$table = $latestRelationshipModel->getTable();
if ($aliases) {
if (is_string($aliases)) {
$table = $aliases;
}
if (is_array($aliases) && array_key_exists($latestRelationshipName, $aliases)) {
$alias = $aliases[$latestRelationshipName];
if (is_callable($alias)) {
$join = collect($this->query->joins)
->whereInstanceOf(PowerJoinClause::class)
->firstWhere('tableName', $table);
$table = $join->alias;
}
}
}
if ($aggregation) {
if (is_null($this->getSelect())) {
$this->select(sprintf('%s.*', $this->getModel()->getTable()));
}
$aliasName = sprintf(
'%s_%s_%s',
$table,
$column,
$aggregation
);
$this->selectRaw(
sprintf(
'%s(%s.%s) as %s',
$aggregation,
$table,
$column,
$aliasName
)
)
->groupBy(sprintf('%s.%s', $this->getModel()->getTable(), $this->getModel()->getKeyName()))
->orderBy(DB::raw(sprintf('%s', $aliasName)), $direction);
} else {
if ($column instanceof Expression) {
$this->orderBy($column, $direction);
} else {
$this->orderBy(
sprintf('%s.%s', $table, $column),
$direction
);
}
}
return $this;
};
}
public function orderByLeftPowerJoins(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, joinType: 'leftJoin');
};
}
/**
* Order by the COUNT aggregation using joins.
*/
public function orderByPowerJoinsCount(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'COUNT');
};
}
public function orderByLeftPowerJoinsCount(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'COUNT', joinType: 'leftJoin');
};
}
/**
* Order by the SUM aggregation using joins.
*/
public function orderByPowerJoinsSum(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'SUM');
};
}
public function orderByLeftPowerJoinsSum(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'SUM', joinType: 'leftJoin');
};
}
/**
* Order by the AVG aggregation using joins.
*/
public function orderByPowerJoinsAvg(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'AVG');
};
}
public function orderByLeftPowerJoinsAvg(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'AVG', joinType: 'leftJoin');
};
}
/**
* Order by the MIN aggregation using joins.
*/
public function orderByPowerJoinsMin(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'MIN');
};
}
public function orderByLeftPowerJoinsMin(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'MIN', joinType: 'leftJoin');
};
}
/**
* Order by the MAX aggregation using joins.
*/
public function orderByPowerJoinsMax(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'MAX');
};
}
public function orderByLeftPowerJoinsMax(): Closure
{
return function (string|array $sort, string $direction = 'asc') {
return $this->orderByPowerJoins(sort: $sort, direction: $direction, aggregation: 'MAX', joinType: 'leftJoin');
};
}
/**
* Same as Laravel 'has`, but using joins instead of where exists.
*/
public function powerJoinHas(): Closure
{
return function (string $relation, string $operator = '>=', int $count = 1, $boolean = 'and', Closure|array|string|null $callback = null, ?string $morphable = null): static {
if (is_null($this->getSelect())) {
$defaultSelect = sprintf('%s.*', $this->getModel()->getTable());
$this->getQuery()->beforeQuery(function ($queryBuilder) use ($defaultSelect) {
if (is_null($queryBuilder->columns) || $queryBuilder->columns === ['*']) {
$queryBuilder->columns = [$defaultSelect];
}
});
}
if (is_null($this->getGroupBy())) {
$this->groupBy($this->getModel()->getQualifiedKeyName());
}
if (is_string($relation)) {
if (Str::contains($relation, '.')) {
$this->hasNestedUsingJoins($relation, $operator, $count, 'and', $callback);
return $this;
}
$relation = $this->getRelationWithoutConstraintsProxy($relation);
}
$relation->performJoinForEloquentPowerJoins($this, 'leftPowerJoin', $callback, morphable: $morphable, hasCheck: true);
$relation->performHavingForEloquentPowerJoins($this, $operator, $count, morphable: $morphable);
return $this;
};
}
public function hasNestedUsingJoins(): Closure
{
return function (string $relations, string $operator = '>=', int $count = 1, string $boolean = 'and', Closure|array|string|null $callback = null): static {
$relations = explode('.', $relations);
/** @var Relation */
$latestRelation = null;
foreach ($relations as $index => $relation) {
$relationName = $relation;
if (!$latestRelation) {
$relation = $this->getRelationWithoutConstraintsProxy($relation);
} else {
$relation = $latestRelation->getModel()->query()->getRelationWithoutConstraintsProxy($relation);
}
$relation->performJoinForEloquentPowerJoins($this, 'leftPowerJoin', is_callable($callback) ? $callback : $callback[$relationName] ?? null);
if (count($relations) === ($index + 1)) {
$relation->performHavingForEloquentPowerJoins($this, $operator, $count);
}
$latestRelation = $relation;
}
return $this;
};
}
public function powerJoinDoesntHave(): Closure
{
return function ($relation, $boolean = 'and', ?Closure $callback = null) {
return $this->powerJoinHas($relation, '<', 1, $boolean, $callback);
};
}
public function powerJoinWhereHas(): Closure
{
return function ($relation, $callback = null, $operator = '>=', $count = 1) {
return $this->powerJoinHas($relation, $operator, $count, 'and', $callback);
};
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Kirschbaum\PowerJoins\Mixins;
class QueryBuilderExtraMethods
{
public function getGroupBy()
{
return function () {
return $this->groups;
};
}
public function getSelect()
{
return function () {
return $this->columns;
};
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Kirschbaum\PowerJoins\Mixins;
use Illuminate\Database\Eloquent\Relations\Relation;
class QueryRelationshipExistence
{
public function getGroupBy()
{
return function () {
return $this->getQuery()->getGroupBy();
};
}
public function getScopes()
{
return function () {
return $this->scopes;
};
}
public function getSelect()
{
return function () {
return $this->getQuery()->getSelect();
};
}
protected function getRelationWithoutConstraintsProxy()
{
return function ($relation) {
return Relation::noConstraints(function () use ($relation) {
return $this->getModel()->{$relation}();
});
};
}
}

View file

@ -0,0 +1,601 @@
<?php
namespace Kirschbaum\PowerJoins\Mixins;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\MySqlConnection;
use Illuminate\Support\Str;
use Kirschbaum\PowerJoins\PowerJoinClause;
use Kirschbaum\PowerJoins\StaticCache;
/**
* @method \Illuminate\Database\Eloquent\Model getModel()
* @method string getTable()
* @method string getForeignPivotKeyName()
* @method string getRelatedPivotKeyName()
* @method bool isOneOfMany()
* @method \Illuminate\Database\Eloquent\Builder|void getOneOfManySubQuery()
* @method \Illuminate\Database\Eloquent\Builder getQuery()
* @method \Illuminate\Database\Eloquent\Model getThroughParent()
* @method string getForeignKeyName()
* @method string getMorphType()
* @method string getMorphClass()
* @method string getFirstKeyName()
* @method string getQualifiedLocalKeyName()
* @method string getExistenceCompareKey()
*
* @mixin \Illuminate\Database\Eloquent\Relations\Relation
* @mixin \Illuminate\Database\Eloquent\Relations\HasOneOrMany
* @mixin \Illuminate\Database\Eloquent\Relations\BelongsToMany
*
* @property \Illuminate\Database\Eloquent\Builder $query
* @property Model $parent
* @property Model $throughParent
* @property string $foreignKey
* @property string $parentKey
* @property string $ownerKey
* @property string $localKey
* @property string $secondKey
* @property string $secondLocalKey
* @property Model $farParent
*/
class RelationshipsExtraMethods
{
/**
* Perform the JOIN clause for eloquent power joins.
*/
public function performJoinForEloquentPowerJoins()
{
return function ($builder, $joinType = 'leftJoin', $callback = null, $alias = null, bool $disableExtraConditions = false, ?string $morphable = null, bool $hasCheck = false) {
return match (true) {
$this instanceof MorphToMany => $this->performJoinForEloquentPowerJoinsForMorphToMany($builder, $joinType, $callback, $alias, $disableExtraConditions),
$this instanceof BelongsToMany => $this->performJoinForEloquentPowerJoinsForBelongsToMany($builder, $joinType, $callback, $alias, $disableExtraConditions),
$this instanceof MorphOneOrMany => $this->performJoinForEloquentPowerJoinsForMorph($builder, $joinType, $callback, $alias, $disableExtraConditions),
$this instanceof HasMany || $this instanceof HasOne => $this->performJoinForEloquentPowerJoinsForHasMany($builder, $joinType, $callback, $alias, $disableExtraConditions, $hasCheck),
$this instanceof HasManyThrough || $this instanceof HasOneThrough => $this->performJoinForEloquentPowerJoinsForHasManyThrough($builder, $joinType, $callback, $alias, $disableExtraConditions),
$this instanceof MorphTo => $this->performJoinForEloquentPowerJoinsForMorphTo($builder, $joinType, $callback, $alias, $disableExtraConditions, $morphable),
default => $this->performJoinForEloquentPowerJoinsForBelongsTo($builder, $joinType, $callback, $alias, $disableExtraConditions),
};
};
}
/**
* Perform the JOIN clause for the BelongsTo (or similar) relationships.
*/
protected function performJoinForEloquentPowerJoinsForBelongsTo()
{
return function ($query, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
$joinedTable = $this->query->getModel()->getTable();
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$query->{$joinType}($joinedTable, function ($join) use ($callback, $joinedTable, $parentTable, $alias, $disableExtraConditions) {
if ($alias) {
$join->as($alias);
}
$join->on(
"{$parentTable}.{$this->foreignKey}",
'=',
"{$joinedTable}.{$this->ownerKey}"
);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull("{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}");
}
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if ($callback && is_callable($callback)) {
$callback($join);
}
}, $this->query->getModel());
};
}
/**
* Perform the JOIN clause for the BelongsToMany (or similar) relationships.
*/
protected function performJoinForEloquentPowerJoinsForBelongsToMany()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
[$alias1, $alias2] = $alias;
$joinedTable = $alias1 ?: $this->getTable();
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$builder->{$joinType}($this->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias1) {
if ($alias1) {
$join->as($alias1);
}
$join->on(
"{$joinedTable}.{$this->getForeignPivotKeyName()}",
'=',
"{$parentTable}.{$this->parentKey}"
);
if (is_array($callback) && isset($callback[$this->getTable()])) {
$callback[$this->getTable()]($join);
}
});
$builder->{$joinType}($this->getModel()->getTable(), function ($join) use ($callback, $joinedTable, $alias2, $disableExtraConditions) {
if ($alias2) {
$join->as($alias2);
}
$join->on(
"{$this->getModel()->getTable()}.{$this->getRelatedKeyName()}",
'=',
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
}
// applying any extra conditions to the belongs to many relationship
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if (is_array($callback) && isset($callback[$this->getModel()->getTable()])) {
$callback[$this->getModel()->getTable()]($join);
}
}, $this->getModel());
return $this;
};
}
/**
* Perform the JOIN clause for the MorphToMany (or similar) relationships.
*/
protected function performJoinForEloquentPowerJoinsForMorphToMany()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
[$alias1, $alias2] = $alias;
$joinedTable = $alias1 ?: $this->getTable();
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$builder->{$joinType}($this->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias1, $disableExtraConditions) {
if ($alias1) {
$join->as($alias1);
}
$join->on(
"{$joinedTable}.{$this->getForeignPivotKeyName()}",
'=',
"{$parentTable}.{$this->parentKey}"
);
// applying any extra conditions to the belongs to many relationship
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if (is_array($callback) && isset($callback[$this->getTable()])) {
$callback[$this->getTable()]($join);
}
});
$builder->{$joinType}($this->getModel()->getTable(), function ($join) use ($callback, $joinedTable, $alias2, $disableExtraConditions) {
if ($alias2) {
$join->as($alias2);
}
$join->on(
"{$this->getModel()->getTable()}.{$this->getModel()->getKeyName()}",
'=',
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
}
if (is_array($callback) && isset($callback[$this->getModel()->getTable()])) {
$callback[$this->getModel()->getTable()]($join);
}
}, $this->getModel());
return $this;
};
}
/**
* Perform the JOIN clause for the Morph (or similar) relationships.
*/
protected function performJoinForEloquentPowerJoinsForMorph()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$builder->{$joinType}($this->getModel()->getTable(), function ($join) use ($callback, $disableExtraConditions, $alias, $parentTable) {
if ($alias) {
$join->as($alias);
}
$join->on(
"{$this->getModel()->getTable()}.{$this->getForeignKeyName()}",
'=',
"{$parentTable}.{$this->localKey}"
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $this->getMorphClass());
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
}
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if ($callback && is_callable($callback)) {
$callback($join);
}
}, $this->getModel());
return $this;
};
}
/**
* Perform the JOIN clause for when calling the morphTo method from the morphable class.
*/
protected function performJoinForEloquentPowerJoinsForMorphTo()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, ?string $morphable = null) {
/** @var Model */
$modelInstance = new $morphable();
$builder->{$joinType}($modelInstance->getTable(), function ($join) use ($modelInstance, $callback, $disableExtraConditions) {
$join->on(
"{$this->getModel()->getTable()}.{$this->getForeignKeyName()}",
'=',
"{$modelInstance->getTable()}.{$modelInstance->getKeyName()}"
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $modelInstance->getMorphClass());
if ($disableExtraConditions === false && $this->usesSoftDeletes($modelInstance->getScopes())) {
$join->whereNull($modelInstance->getQualifiedDeletedAtColumn());
}
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if ($callback && is_callable($callback)) {
$callback($join);
}
}, $modelInstance);
return $this;
};
}
/**
* Perform the JOIN clause for the HasMany (or similar) relationships.
*/
protected function performJoinForEloquentPowerJoinsForHasMany()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, bool $hasCheck = false) {
$joinedModel = $this->query->getModel();
$joinedTable = $alias ?: $joinedModel->getTable();
$parentTable = StaticCache::getTableOrAliasForModel($this->parent);
$isOneOfMany = method_exists($this, 'isOneOfMany') ? $this->isOneOfMany() : false;
if ($isOneOfMany && !$hasCheck) {
$column = $this->getOneOfManySubQuery()->getQuery()->columns[0];
$fkColumn = $this->getOneOfManySubQuery()->getQuery()->columns[1];
$localKey = $this->localKey;
$builder->where(function ($query) use ($column, $joinType, $joinedModel, $builder, $fkColumn, $parentTable, $localKey, $disableExtraConditions) {
$query->whereIn($joinedModel->getQualifiedKeyName(), function ($query) use ($column, $joinedModel, $builder, $fkColumn, $parentTable, $localKey, $disableExtraConditions) {
$columnValue = $column->getValue($builder->getGrammar());
$direction = Str::contains($columnValue, 'min(') ? 'asc' : 'desc';
$columnName = Str::of($columnValue)->after('(')->before(')')->__toString();
$columnName = Str::replace(['"', "'", '`'], '', $columnName);
if ($builder->getConnection() instanceof MySqlConnection) {
$query->select('*')->from(function ($query) use ($joinedModel, $columnName, $fkColumn, $direction, $parentTable, $localKey, $disableExtraConditions) {
$query
->select($joinedModel->getQualifiedKeyName())
->from($joinedModel->getTable())
->whereColumn($fkColumn, "{$parentTable}.{$localKey}")
->orderBy($columnName, $direction)
->take(1);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$query->whereNull($this->query->getModel()->getDeletedAtColumn());
}
});
} else {
$query
->select($joinedModel->getQualifiedKeyName())
->distinct($columnName)
->from($joinedModel->getTable())
->whereColumn($fkColumn, "{$parentTable}.{$localKey}")
->orderBy($columnName, $direction)
->take(1);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$query->whereNull($this->query->getModel()->getDeletedAtColumn());
}
}
});
if ($joinType === 'leftPowerJoin') {
$query->orWhereRaw('1 = 1');
}
});
}
$builder->{$joinType}($this->query->getModel()->getTable(), function ($join) use ($callback, $joinedTable, $parentTable, $alias, $disableExtraConditions) {
if ($alias) {
$join->as($alias);
}
$join->on(
$this->foreignKey,
'=',
"{$parentTable}.{$this->localKey}"
);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
$join->whereNull(
"{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}"
);
}
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if ($callback && is_callable($callback)) {
$callback($join);
}
}, $this->query->getModel());
};
}
/**
* Perform the JOIN clause for the HasManyThrough relationships.
*/
protected function performJoinForEloquentPowerJoinsForHasManyThrough()
{
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false) {
[$alias1, $alias2] = $alias;
$throughTable = $alias1 ?: $this->getThroughParent()->getTable();
$farTable = $alias2 ?: $this->getModel()->getTable();
$builder->{$joinType}($this->getThroughParent()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $alias1, $disableExtraConditions) {
if ($alias1) {
$join->as($alias1);
}
$farParentTable = StaticCache::getTableOrAliasForModel($this->getFarParent());
$join->on(
"{$throughTable}.{$this->getFirstKeyName()}",
'=',
"{$farParentTable}.{$this->localKey}"
);
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->getThroughParent())) {
$join->whereNull($this->getThroughParent()->getQualifiedDeletedAtColumn());
}
if ($disableExtraConditions === false) {
$this->applyExtraConditions($join);
}
if (is_array($callback) && isset($callback[$this->getThroughParent()->getTable()])) {
$callback[$this->getThroughParent()->getTable()]($join);
}
if ($callback && is_callable($callback)) {
$callback($join);
}
}, $this->getThroughParent());
$builder->{$joinType}($this->getModel()->getTable(), function (PowerJoinClause $join) use ($callback, $throughTable, $farTable, $alias2) {
if ($alias2) {
$join->as($alias2);
}
$join->on(
"{$farTable}.{$this->secondKey}",
'=',
"{$throughTable}.{$this->secondLocalKey}"
);
if ($this->usesSoftDeletes($this->getScopes())) {
$join->whereNull("{$farTable}.{$this->getModel()->getDeletedAtColumn()}");
}
if (is_array($callback) && isset($callback[$this->getModel()->getTable()])) {
$callback[$this->getModel()->getTable()]($join);
}
}, $this->getModel());
return $this;
};
}
/**
* Perform the "HAVING" clause for eloquent power joins.
*/
public function performHavingForEloquentPowerJoins()
{
return function ($builder, $operator, $count, ?string $morphable = null) {
if (is_null($builder->getSelect())) {
$builder->select(sprintf('%s.*', $builder->getModel()->getTable()));
}
if ($morphable) {
$modelInstance = new $morphable();
$builder
->selectRaw(sprintf('count(%s) as %s_count', $modelInstance->getQualifiedKeyName(), Str::replace('.', '_', $modelInstance->getTable())))
->havingRaw(sprintf('count(%s) %s %d', $modelInstance->getQualifiedKeyName(), $operator, $count));
} else {
$builder
->selectRaw(sprintf('count(%s) as %s_count', $this->query->getModel()->getQualifiedKeyName(), Str::replace('.', '_', $this->query->getModel()->getTable())))
->havingRaw(sprintf('count(%s) %s %d', $this->query->getModel()->getQualifiedKeyName(), $operator, $count));
}
};
}
/**
* Checks if the relationship model uses soft deletes.
*/
public function usesSoftDeletes()
{
/*
* @param \Illuminate\Database\Eloquent\Model|array $model
*/
return function ($model) {
if ($model instanceof Model) {
return in_array(SoftDeletes::class, class_uses_recursive($model), true);
}
return array_key_exists(SoftDeletingScope::class, $model);
};
}
/**
* Get the throughParent for the HasManyThrough relationship.
*/
public function getThroughParent()
{
return function () {
return $this->throughParent;
};
}
/**
* Get the farParent for the HasManyThrough relationship.
*/
public function getFarParent()
{
return function () {
return $this->farParent;
};
}
public function applyExtraConditions()
{
return function (PowerJoinClause $join) {
foreach ($this->getQuery()->getQuery()->wheres as $condition) {
if ($this->shouldNotApplyExtraCondition($condition)) {
continue;
}
if (!in_array($condition['type'], ['Basic', 'Null', 'NotNull', 'Nested'], true)) {
continue;
}
$method = "apply{$condition['type']}Condition";
$this->$method($join, $condition);
}
};
}
public function applyBasicCondition()
{
return function ($join, $condition) {
$join->where($condition['column'], $condition['operator'], $condition['value'], $condition['boolean']);
};
}
public function applyNullCondition()
{
return function ($join, $condition) {
$join->whereNull($condition['column'], $condition['boolean']);
};
}
public function applyNotNullCondition()
{
return function ($join, $condition) {
$join->whereNotNull($condition['column'], $condition['boolean']);
};
}
public function applyNestedCondition()
{
return function ($join, $condition) {
$join->where(function ($q) use ($condition) {
foreach ($condition['query']->wheres as $condition) {
$method = "apply{$condition['type']}Condition";
$this->$method($q, $condition);
}
});
};
}
public function shouldNotApplyExtraCondition()
{
return function ($condition) {
if (isset($condition['column']) && ($condition['column'] === '' || Str::endsWith($condition['column'], '.'))) {
return true;
}
if (!$key = $this->getPowerJoinExistenceCompareKey()) {
return true;
}
if (isset($condition['query'])) {
return false;
}
if (is_array($key)) {
return in_array($condition['column'], $key, true);
}
return $condition['column'] === $key;
};
}
public function getPowerJoinExistenceCompareKey()
{
return function () {
if ($this instanceof MorphTo) {
return [$this->getMorphType(), $this->getForeignKeyName()];
}
if ($this instanceof BelongsTo) {
return $this->getQualifiedOwnerKeyName();
}
if ($this instanceof HasMany || $this instanceof HasOne) {
return $this->getExistenceCompareKey();
}
if ($this instanceof HasManyThrough || $this instanceof HasOneThrough) {
return $this->getQualifiedFirstKeyName();
}
if ($this instanceof BelongsToMany) {
return $this->getExistenceCompareKey();
}
if ($this instanceof MorphOneOrMany) {
return [$this->getQualifiedMorphType(), $this->getExistenceCompareKey()];
}
};
}
}

View file

@ -0,0 +1,340 @@
<?php
namespace Kirschbaum\PowerJoins;
use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Str;
use InvalidArgumentException;
use ReflectionClass;
class PowerJoinClause extends JoinClause
{
/**
* @var Model
*/
public $model;
/**
* Table name backup in case an alias is being used.
*
* @var string
*/
public $tableName;
/**
* Alias name.
*/
public ?string $alias = null;
/**
* Joined table alias name (mostly for belongs to many aliases).
*/
public ?string $joinedTableAlias = null;
/**
* Create a new join clause instance.
*/
public function __construct(Builder $parentQuery, $type, string $table, ?Model $model = null)
{
parent::__construct($parentQuery, $type, $table);
$this->model = $model;
$this->tableName = $table;
}
/**
* Add an alias to the table being joined.
*/
public function as(string $alias, ?string $joinedTableAlias = null): self
{
$this->alias = $alias;
$this->joinedTableAlias = $joinedTableAlias;
$this->table = sprintf('%s as %s', $this->table, $alias);
$this->useTableAliasInConditions();
if ($this->model) {
StaticCache::setTableAliasForModel($this->model, $alias);
}
return $this;
}
public function on($first, $operator = null, $second = null, $boolean = 'and'): self
{
parent::on($first, $operator, $second, $boolean);
$this->useTableAliasInConditions();
return $this;
}
public function getModel()
{
return $this->model;
}
/**
* Apply the global scopes to the joined query.
*/
public function withGlobalScopes(): self
{
if (!$this->model) {
return $this;
}
foreach ($this->model->getGlobalScopes() as $scope) {
if ($scope instanceof Closure) {
$scope->call($this, $this);
continue;
}
if ($scope instanceof SoftDeletingScope) {
continue;
}
(new $scope())->apply($this, $this->model);
}
return $this;
}
/**
* Apply the table alias in the existing join conditions.
*/
protected function useTableAliasInConditions(): self
{
if (!$this->alias || !$this->model) {
return $this;
}
$this->wheres = collect($this->wheres)->filter(function ($where) {
$whereType = $where['type'] ?? '';
if (in_array($whereType, ['Column', 'Basic'], true)) {
return true;
}
if ($whereType === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
return true;
}
return false;
})->map(function ($where) {
if ($where['type'] === 'Null') {
return $where;
}
$key = $this->model->getKeyName();
$table = $this->tableName;
$replaceMethod = sprintf('useAliasInWhere%sType', ucfirst($where['type']));
return $this->{$replaceMethod}($where);
})->toArray();
return $this;
}
protected function useAliasInWhereColumnType(array $where): array
{
$key = $this->model->getKeyName();
$table = $this->tableName;
// if it was already replaced, skip
if (Str::startsWith($where['first'].'.', $this->alias.'.') || Str::startsWith($where['second'].'.', $this->alias.'.')) {
return $where;
}
if (Str::contains($where['first'], $table) && Str::contains($where['second'], $table)) {
// if joining the same table, only replace the correct table.key pair
$where['first'] = str_replace($table.'.'.$key, $this->alias.'.'.$key, $where['first']);
$where['second'] = str_replace($table.'.'.$key, $this->alias.'.'.$key, $where['second']);
} else {
$where['first'] = str_replace($table.'.', $this->alias.'.', $where['first']);
$where['second'] = str_replace($table.'.', $this->alias.'.', $where['second']);
}
return $where;
}
protected function useAliasInWhereBasicType(array $where): array
{
$table = $this->tableName;
if (Str::startsWith($where['column'].'.', $this->alias.'.')) {
return $where;
}
if (Str::contains($where['column'], $table)) {
// if joining the same table, only replace the correct table.key pair
$where['column'] = str_replace($table.'.', $this->alias.'.', $where['column']);
} else {
$where['column'] = str_replace($table.'.', $this->alias.'.', $where['column']);
}
return $where;
}
public function whereNull($columns, $boolean = 'and', $not = false)
{
if ($this->alias && Str::contains($columns, $this->tableName)) {
$columns = str_replace("{$this->tableName}.", "{$this->alias}.", $columns);
}
return parent::whereNull($columns, $boolean, $not);
}
public function newQuery(): self
{
return new static($this->newParentQuery(), $this->type, $this->table, $this->model); // <-- The model param is needed
}
public function where($column, $operator = null, $value = null, $boolean = 'and'): self
{
if ($this->alias && is_string($column) && Str::contains($column, $this->tableName)) {
$column = str_replace("{$this->tableName}.", "{$this->alias}.", $column);
} elseif ($this->alias && !is_callable($column)) {
$column = $this->alias.'.'.$column;
}
if (is_callable($column)) {
$query = new self($this, $this->type, $this->table, $this->model);
$column($query);
return $this->addNestedWhereQuery($query);
}
return parent::where($column, $operator, $value, $boolean);
}
/**
* Remove the soft delete condition in case the model implements soft deletes.
*/
public function withTrashed(): self
{
if (!$this->getModel() || !in_array(SoftDeletes::class, class_uses_recursive($this->getModel()), true)) {
return $this;
}
$this->wheres = array_filter($this->wheres, function ($where) {
if ($where['type'] === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
return false;
}
return true;
});
return $this;
}
/**
* Remove the soft delete condition in case the model implements soft deletes.
*/
public function onlyTrashed(): self
{
if (!$this->getModel()
|| !in_array(SoftDeletes::class, class_uses_recursive($this->getModel()), true)
) {
return $this;
}
$hasCondition = null;
$this->wheres = array_map(function ($where) use (&$hasCondition) {
if ($where['type'] === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
$where['type'] = 'NotNull';
$hasCondition = true;
}
return $where;
}, $this->wheres);
if (!$hasCondition) {
$this->whereNotNull($this->getModel()->getQualifiedDeletedAtColumn());
}
return $this;
}
public function left(): self
{
return $this->joinType('left');
}
public function right(): self
{
return $this->joinType('right');
}
public function inner(): self
{
return $this->joinType('inner');
}
public function joinType(string $joinType): self
{
$this->type = $joinType;
return $this;
}
public function __call($name, $arguments)
{
if (!$this->getModel()) {
return;
}
if (method_exists($this->getModel(), 'scope'.ucfirst($name))) {
$scope = 'scope'.ucfirst($name);
return $this->getModel()->{$scope}($this, ...$arguments);
}
if ($this->hasLaravelScopeAttribute($name) && version_compare(app()->version(), '12.0.0', '>=')) {
return $this->getModel()->callNamedScope($name, array_merge([$this], $arguments));
}
if (static::hasMacro($name)) {
return $this->macroCall($name, $arguments);
}
$eloquentBuilder = $this->getModel()->newEloquentBuilder($this);
if (method_exists($eloquentBuilder, $name)) {
$eloquentBuilder->setModel($this->getModel());
return $eloquentBuilder->{$name}(...$arguments);
}
throw new InvalidArgumentException(sprintf('Method %s does not exist in PowerJoinClause class', $name));
}
/**
* Check if a method has the Laravel Scope attribute.
*/
protected function hasLaravelScopeAttribute(string $methodName): bool
{
if (!method_exists($this->getModel(), $methodName)) {
return false;
}
$reflection = new ReflectionClass($this->getModel());
if (!$reflection->hasMethod($methodName)) {
return false;
}
$method = $reflection->getMethod($methodName);
$attributes = $method->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getName() === 'Illuminate\Database\Eloquent\Attributes\Scope') {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Kirschbaum\PowerJoins;
use Illuminate\Support\ServiceProvider;
class PowerJoinsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
}
/**
* Register the application services.
*/
public function register()
{
EloquentJoins::registerEloquentMacros();
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Kirschbaum\PowerJoins;
use Illuminate\Database\Eloquent\Model;
class StaticCache
{
/**
* Cache to not join the same relationship twice.
*
* @var array<int, string>
*/
public static array $powerJoinAliasesCache = [];
public static function getTableOrAliasForModel(Model $model): string
{
return static::$powerJoinAliasesCache[spl_object_id($model)] ?? $model->getTable();
}
public static function setTableAliasForModel(Model $model, $alias): void
{
static::$powerJoinAliasesCache[spl_object_id($model)] = $alias;
}
public static function clear(): void
{
static::$powerJoinAliasesCache = [];
}
}