Implement very basic API searching. Closes #1348
This commit is contained in:
committed by
James Brooks
parent
9379ab131c
commit
0b3483fb8a
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) Alt Three Services Limited
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Models\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* This is the searchable trait.
|
||||
*
|
||||
* @author James Brooks <james@alt-three.com>
|
||||
*/
|
||||
trait SearchableTrait
|
||||
{
|
||||
/**
|
||||
* Adds a sort scope.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param array $column
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeSearch(Builder $query, array $search = [])
|
||||
{
|
||||
if (empty($search)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
if (!array_intersect(array_keys($search), $this->searchable)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->where($search);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user