Я хочу создать автоматический querybuilder, который принимает параметры из url в контроллере и передает его в мой репозиторий.Laravel automatic querybuilder от url
Теперь у меня есть набор ключей/фильтров, которые запрос может использовать для построения предложения where, он соответствует ключам к параметрам в URL-адресе и работает. Пока я использую только 1 ключ.
если я f.e. do:
".../api/v1/companies?is_accountant=0" -->WORKS
".../api/v1/companies?is_accountant=0&name=test&street=testLane -->FAILS, returns empty
Запись существует в моей БД, но она возвращает пустой объект. Если я попытаюсь разобраться с 1 ключом, он найдет запись
Может ли кто-нибудь выявить проблему?
private $filters = ['id','street','name','is_accountant','nr_ext','postal_code'];
public function getCompaniesFilterd($params, $limit)
{
if (!empty($params)) {
$query = Company::query();
foreach($params as $param)
{
foreach($this->filters as $filter)
{
if(key($params) == $filter)
{
$query->where($filter,'=',$param);
}
}
}
$companies = $query->get();
dd($query);
return $companies;
}
return Company::take(10)->get();
}
выход
{
"data": []
}
Запрос дамп
object(Illuminate\Database\Eloquent\Builder)[222]
protected 'query' =>
object(Illuminate\Database\Query\Builder)[221]
protected 'connection' =>
object(Illuminate\Database\MySqlConnection)[212]
protected 'pdo' =>
object(PDO)[181]
...
protected 'readPdo' => null
protected 'reconnector' =>
object(Closure)[220]
...
protected 'queryGrammar' =>
object(Illuminate\Database\Query\Grammars\MySqlGrammar)[211]
...
protected 'schemaGrammar' => null
protected 'postProcessor' =>
object(Illuminate\Database\Query\Processors\MySqlProcessor)[214]
...
protected 'events' =>
object(Illuminate\Events\Dispatcher)[14]
...
protected 'paginator' =>
object(Closure)[219]
...
protected 'cache' =>
object(Closure)[218]
...
protected 'fetchMode' => int 8
protected 'transactions' => int 0
protected 'queryLog' =>
array (size=1)
...
protected 'loggingQueries' => boolean true
protected 'pretending' => boolean false
protected 'database' => string 'homestead' (length=9)
protected 'tablePrefix' => string '' (length=0)
protected 'config' =>
array (size=9)
...
protected 'grammar' =>
object(Illuminate\Database\Query\Grammars\MySqlGrammar)[211]
protected 'selectComponents' =>
array (size=11)
...
protected 'tablePrefix' => string '' (length=0)
protected 'processor' =>
object(Illuminate\Database\Query\Processors\MySqlProcessor)[214]
protected 'bindings' =>
array (size=5)
'select' =>
array (size=0)
...
'join' =>
array (size=0)
...
'where' =>
array (size=3)
...
'having' =>
array (size=0)
...
'order' =>
array (size=0)
...
public 'aggregate' => null
public 'columns' =>
array (size=1)
0 => string '*' (length=1)
public 'distinct' => boolean false
public 'from' => string 'companies' (length=9)
public 'joins' => null
public 'wheres' =>
array (size=3)
0 =>
array (size=5)
...
1 =>
array (size=5)
...
2 =>
array (size=5)
...
public 'groups' => null
public 'havings' => null
public 'orders' => null
public 'limit' => null
public 'offset' => null
public 'unions' => null
public 'lock' => null
protected 'backups' =>
array (size=0)
empty
protected 'cacheKey' => null
protected 'cacheMinutes' => null
protected 'cacheTags' => null
protected 'cacheDriver' => null
protected 'operators' =>
array (size=19)
0 => string '=' (length=1)
1 => string '<' (length=1)
2 => string '>' (length=1)
3 => string '<=' (length=2)
4 => string '>=' (length=2)
5 => string '<>' (length=2)
6 => string '!=' (length=2)
7 => string 'like' (length=4)
8 => string 'not like' (length=8)
9 => string 'between' (length=7)
10 => string 'ilike' (length=5)
11 => string '&' (length=1)
12 => string '|' (length=1)
13 => string '^' (length=1)
14 => string '<<' (length=2)
15 => string '>>' (length=2)
16 => string 'rlike' (length=5)
17 => string 'regexp' (length=6)
18 => string 'not regexp' (length=10)
protected 'model' =>
object(Winbooks\Core\Models\Company)[213]
protected 'table' => string 'companies' (length=9)
protected 'fillable' =>
array (size=10)
0 => string 'name' (length=4)
1 => string 'street' (length=6)
2 => string 'nr_ext' (length=6)
3 => string 'postal_code' (length=11)
4 => string 'city' (length=4)
5 => string 'phone' (length=5)
6 => string 'fax' (length=3)
7 => string 'email' (length=5)
8 => string 'is_accountant' (length=13)
9 => string 'country_id' (length=10)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=0)
empty
protected 'original' =>
array (size=0)
empty
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean false
protected 'eagerLoad' =>
array (size=0)
empty
protected 'macros' =>
array (size=0)
empty
protected 'onDelete' => null
protected 'passthru' =>
array (size=12)
0 => string 'toSql' (length=5)
1 => string 'lists' (length=5)
2 => string 'insert' (length=6)
3 => string 'insertGetId' (length=11)
4 => string 'pluck' (length=5)
5 => string 'count' (length=5)
6 => string 'min' (length=3)
7 => string 'max' (length=3)
8 => string 'avg' (length=3)
9 => string 'sum' (length=3)
10 => string 'exists' (length=6)
11 => string 'getBindings' (length=11)
Вы не должны ставить 'test' в кавычки, если запись базы данных не совсем так (' «кавычки» '). но это имя, вероятно, 'test', а не' 'test" '. – Quasdunk
Это действительно проблема № 1. Пробовал ли он без кавычек, все еще не работает? – Nsmet
Неудачно или возвращается пустым? эти две вещи НЕ совпадают. – itachi