我们平时使用Laravel的Model查询当查询应用了多个条件的时候一般都是链式调用,像下面这样:
User::where('age', '=', 18)->where('sex', '=', 'male')->where('nation' '=', 'US')->get();
不过在做数据搜索的时候往往会遇到根据搜索参数应用不同的查询条件,比如说参数里有age
值时才会给查询应用上age = value
的条件搜索参数里没有age
的话不对搜索的年龄进行限制。那么在Laravel
里如何优雅地让查询根据参数条件应用上不同的where条件呢?我们来看一下
$query = User::newQuery();
$params['age'] && $query->where('age', '=', $param['age']);
$params['sex'] && $query->where('sex', '=', $params['sex']);
$params['nation'] && $query->where('nation', '=', $params['nation']);
$query->get();
laravel session,这样就能根据参数来为查询应用不同的条件来,不过还不够优雅,我们把这几个where条件封装到Model的查询作用域中,这样在其它地方应用到这些查询的时候就不用再重复地写一堆where条件了
//User Model
public function scopeWhereAge($query, $age)
{return $query->where('age', '=', $age);
}public function scopeWhereSex($query, $sex)
{return $query->where('sex', '=', $sex);
}public function scopeWhereNation($query, $nation)
{return $query->where('nation', '=', $nation)
}
接下来我们把之前代码块里的where条件都换成调用查询作用域的形式:
$query = User::query();
$params['age'] && $query->whereAge($param['age']);
$params['sex'] && $query->whereSex($params['sex']);
$params['nation'] && $query->whereNation($params['nation']);
$query->get();
为了方便调用我们把上面这个代码块封装到UserSerivce
里
namespace App\Services;
use App\Models\User;
class UserService
{public function userList($params){$query = User::query();$params['age'] && $query->whereAge($param['age']);$params['sex'] && $query->whereSex($params['sex']);$params['nation'] && $query->whereNation($params['nation']);//$query->toSql();查看生成的SQLreturn $query->get();}
}
php laravel。之后在用到搜索获取用户列表的地方注入UserService
就能调用UserService
的userList
方法获取用户列表了。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态