forked from bosnadev/repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a3ad68
commit 3fb97f4
Showing
2 changed files
with
41 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,13 @@ public function find($id, $columns = ['*']); | |
|
||
/** | ||
* @param $attribute | ||
* @param $value | ||
* @param string $value | ||
* @param array $columns | ||
* @param bool $or | ||
* @return mixed | ||
* @author Taylor <[email protected]> | ||
*/ | ||
public function findBy($attribute, $value = '', $columns = ['*']); | ||
public function findBy($attribute, $value = '', $columns = ['*'], $or = false); | ||
|
||
/** | ||
* @param $field | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,15 +152,49 @@ public function find($id, $columns = ['*']) | |
* @param array $columns | ||
* @return mixed | ||
*/ | ||
public function findBy($attribute, $value = '', $columns = ['*']) | ||
/** | ||
* @param $attribute | ||
* @param string $value | ||
* @param array $columns | ||
* @param bool $or | ||
* @return mixed | ||
* @author Taylor <[email protected]> | ||
*/ | ||
public function findBy($attribute, $value = '', $columns = ['*'], $or = false) | ||
{ | ||
$this->applyCriteria(); | ||
|
||
$model = $this->model; | ||
|
||
if (is_array($attribute)) { | ||
return $this->model->where($attribute)->first($columns); | ||
foreach ($attribute as $field => $value) { | ||
if ($value instanceof \Closure) { | ||
$model = (!$or) | ||
? $model->where($value) | ||
: $model->orWhere($value); | ||
} elseif (is_array($value)) { | ||
if (count($value) === 3) { | ||
list($field, $operator, $search) = $value; | ||
$model = (!$or) | ||
? $model->where($field, $operator, $search) | ||
: $model->orWhere($field, $operator, $search); | ||
} elseif (count($value) === 2) { | ||
list($field, $search) = $value; | ||
$model = (!$or) | ||
? $model->where($field, '=', $search) | ||
: $model->orWhere($field, '=', $search); | ||
} | ||
} else { | ||
$model = (!$or) | ||
? $model->where($field, '=', $value) | ||
: $model->orWhere($field, '=', $value); | ||
} | ||
} | ||
|
||
return $model->first($columns); | ||
} | ||
|
||
return $this->model->where($attribute, '=', $value)->first($columns); | ||
return $model->where($attribute, '=', $value)->first($columns); | ||
} | ||
|
||
/** | ||
|