首页 > 其他 > 详细

tp5怎么关联模型进行条件查询?

时间:2020-05-13 19:04:35      阅读:409      评论:0      收藏:0      [点我收藏+]

?

// 查询用户昵称是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‘));

?

?

文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。

?

若有帮助到您,欢迎点击推荐,您的支持是对我坚持最好的肯定(*^_^*)

tp5怎么关联模型进行条件查询?

原文:https://www.cnblogs.com/lovebing/p/12884086.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!