Я попытался первый подход из предыдущего ответа, прежде чем задать вопрос, но это не удалось, так что я думал, что это было ошибка. Но, увидев и этот ответ, казалось, что я ничего не понимаю. Итак, я выкопал внутреннюю часть Laravel Eloquent, и вот что я нашел:
Eloquent Класс класса «:: where» изменяет свойство this this this this this this this this this this this this is this is this this is this is the request this this is this is this is this is this is this is this is the request this this is this is this is this is the request this this is this is this is this is this is this is this is this is this is this is Вот код:
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof Closure) {
$query = $this->model->newQueryWithoutScopes();
call_user_func($column, $query);
$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
} else {
call_user_func_array([$this->query, 'where'], func_get_args());
}
return $this;
}
замечать "$ this-> query-> addNestedWhereQuery ($ query-> GetQuery(), $ булево);" на 6-й линии.
Метод Builder класса «:: whereHas» не изменяет свойство «this-> query».
public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1)
{
return $this->has($relation, $operator, $count, 'and', $callback);
}
Итак, что это касается моей проблемы. Я создал запрос, как это:
$query = Item::where('active',1);
Затем я добавил условия:
if($meta){
$query->whereHas('metas', function($query) {
$query->where('Meta_id', 1);
$query->where('value', 'new);
})
->whereHas('metas', function($query) {
$query->where('Meta_id', 3);
$query->where('value', 'LCD);
})
}
, а затем запрос была выполнена:
$query->get();
Ошибка была связана с :: whereHas метод , мне нужно было назначить whereHas результат для переменной:
if($meta){
$query = $query->whereHas('metas', function($query) {
$query->where('Meta_id', 1);
$query->where('value', 'new);
})
->whereHas('metas', function($query) {
$query->where('Meta_id', 3);
$query->where('value', 'LCD);
})
}
Вот и все.Всегда проверяйте с внутренними деталями, если что-то работает неудивительно