Skip to content

Commit

Permalink
add ability for different where clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
fico7489 committed Jan 21, 2018
1 parent 6611aae commit 741756e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/Services/QueryNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Services;

class QueryNormalizer
{
public static function normalize($parameters)
{
$firstParam = array_values($parameters)[0];

if (is_array($firstParam)) {
$parametersNew = [];
foreach ($firstParam as $k => $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);
}
}
5 changes: 5 additions & 0 deletions src/Traits/EloquentJoinTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Traits/JoinRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Fico7489\Laravel\EloquentJoin\Traits;

use Fico7489\Laravel\EloquentJoin\Services\QueryNormalizer;

trait JoinRelationTrait
{
/**
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion tests/Models/Seller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 741756e

Please sign in to comment.