0
点赞
收藏
分享

微信扫一扫

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'));


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

你要保守你心,胜过保守一切。

作者:刘俊涛的博客​


举报

相关推荐

0 条评论