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