tp5怎么关联模型进行条件查询?
// 查询用户昵称是think的用户
// 注意第一个参数是关联方法名(不是关联模型名)
$users = User::hasWhere('profile', ['nickname'=>'think'])->select();
// 可以使用闭包查询
$users = User::hasWhere('profile', function($query) {
$query->where('nickname', 'like', 'think%');
})->select();
模型里面
public function authGroupAccess()
{
return $this->belongsTo('authGroupAccess', 'id', 'uid')->bind('group_id');
}
控制器里面:
hasWhere的使用
if (!empty($param['group_id'])) {
$haswhere['group_id'] = $param['group_id'];
}
$list = model('admin')
->hasWhere('authGroupAccess', $haswhere)
->with('authGroupAccess,authGroup')
->where('username', 'neq', 'admin')
->order('id','desc')
->where($where)
->paginate(config('page_number'));
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
若有帮助到您,欢迎点击推荐,您的支持是对我坚持最好的肯定(*^_^*)
你要保守你心,胜过保守一切。
本文来自博客园,作者:刘俊涛的博客,转载请注明原文链接:https://www.cnblogs.com/lovebing/p/12884086.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2017-05-13 Crontab命令--Linux