我的代码是这样的:如何在查询关系存在上添加条件? Laravel 5.3
<?php
public function getFavoriteStore($param = null)
{
$num = 20;
$q = $param['q'];
$location = $param['location'];
$result = $this->store_repository->whereHas('favorites', function ($query) {
$query = $query->where('stores.status', '=', 1)
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
if(isset($location))
$query = $query->where('stores.address', 'like', "%$location%");
if(isset($q)) {
$query = $query->where(function ($query) use ($q) {
$query->where('stores.name', 'like', "%$q%")
->where('stores.address', 'like', "%$q%", 'or');
});
}
return $query;
})->paginate($num);
return $result;
}
它的工作原理
但是,条件如果(if(isset($location))
& if(isset($q))
)不工作
看来还是有错
是有谁可以帮助我?
我按照这个教程:https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence
if(isset($ location))&& if(isset($ q)) – geckob
不要忘记将'location'和'q'注入到第一个闭包中 – geckob
@geckob,Ok。谢谢bro –