From 741756e0ec1f853d94fa03aa638aae3589e49629 Mon Sep 17 00:00:00 2001 From: Filip Horvat Date: Sun, 21 Jan 2018 12:14:25 +0100 Subject: [PATCH] add ability for different where clauses --- src/Services/QueryNormalizer.php | 33 ++++++++++++++++++++++++++++++++ src/Traits/EloquentJoinTrait.php | 5 +++++ src/Traits/JoinRelationTrait.php | 8 ++++++-- tests/Models/Seller.php | 4 +++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/Services/QueryNormalizer.php diff --git a/src/Services/QueryNormalizer.php b/src/Services/QueryNormalizer.php new file mode 100644 index 0000000..7fbf109 --- /dev/null +++ b/src/Services/QueryNormalizer.php @@ -0,0 +1,33 @@ + $v) { + $parametersNew = [$k, '=', $v]; + } + } elseif (count($parameters) == 2) { + $secondParam = array_values($parameters)[1]; + $parametersNew = [$firstParam, '=', $secondParam]; + } else { + $parametersNew = $parameters; + } + + return $parametersNew; + } + + public static function normalizeScope($parameters) + { + unset($parameters[0]); + $parameters = array_values($parameters); + + return self::normalize($parameters); + } +} diff --git a/src/Traits/EloquentJoinTrait.php b/src/Traits/EloquentJoinTrait.php index caece28..3211562 100644 --- a/src/Traits/EloquentJoinTrait.php +++ b/src/Traits/EloquentJoinTrait.php @@ -5,6 +5,7 @@ use Fico7489\Laravel\EloquentJoin\Relations\BelongsToJoin; use Fico7489\Laravel\EloquentJoin\Exceptions\EloquentJoinException; use Fico7489\Laravel\EloquentJoin\Relations\HasOneJoin; +use Fico7489\Laravel\EloquentJoin\Services\QueryNormalizer; use Illuminate\Database\Eloquent\Builder; trait EloquentJoinTrait @@ -58,13 +59,17 @@ public function scopeSetSoftDelete(Builder $builder, $param) public function scopeWhereJoin(Builder $builder, $column, $operator = null, $value = null, $boolean = 'and') { + list($column, $operator, $value) = QueryNormalizer::normalizeScope(func_get_args()); $column = $this->performJoin($builder, $column); + return $builder->where($column, $operator, $value, $boolean); } public function scopeOrWhereJoin(Builder $builder, $column, $operator = null, $value) { + list($column, $operator, $value) = QueryNormalizer::normalizeScope(func_get_args()); $column = $this->performJoin($builder, $column); + return $builder->orWhere($column, $operator, $value); } diff --git a/src/Traits/JoinRelationTrait.php b/src/Traits/JoinRelationTrait.php index ebd88dd..d411029 100644 --- a/src/Traits/JoinRelationTrait.php +++ b/src/Traits/JoinRelationTrait.php @@ -2,6 +2,8 @@ namespace Fico7489\Laravel\EloquentJoin\Traits; +use Fico7489\Laravel\EloquentJoin\Services\QueryNormalizer; + trait JoinRelationTrait { /** @@ -16,9 +18,11 @@ public function __call($method, $parameters) $softDeleteOptions = ['withTrashed', 'withoutTrashed', 'onlyTrashed']; if ($method == 'where') { - parent::__call('setWhereForJoin', $parameters); + $parametersNew = QueryNormalizer::normalize($parameters); + parent::__call('setWhereForJoin', $parametersNew); } elseif ($method == 'orWhere') { - parent::__call('setOrWhereForJoin', $parameters); + $parametersNew = QueryNormalizer::normalize($parameters); + parent::__call('setOrWhereForJoin', $parametersNew); } elseif (in_array($method, $softDeleteOptions)) { parent::__call('setSoftDelete', [$method]); } else { diff --git a/tests/Models/Seller.php b/tests/Models/Seller.php index 3d36c96..0c2c1d8 100644 --- a/tests/Models/Seller.php +++ b/tests/Models/Seller.php @@ -36,7 +36,9 @@ public function locationPrimaryInvalid() public function locationPrimaryInvalid2() { return $this->hasOne(Location::class) - ->where(['is_primary' => 1]); + ->where(function ($query) { + return $query->where(['id' => 1]); + }); } public function locationSecondary()