一对多关联表,怎么判断返回关联数组不为空的值啊?

User表
id、name、phone、created_at、updated_at

Role表
id、title、created_at、updated_at

user_role关联表
id、user_id、role_id

 $users = User::with(['roles' => function ($query) {
                $query->where();
            }])
 ->orderBy('id', 'desc')->paginate(14);

想查询出有角色的用户,这个条件应该怎么写啊?
$query->where();

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5
$users = User::with('firm', 'roles')
                ->has('roles')
                ->orderBy('id', 'desc')->paginate(14);

和 ->where(‘real_name’, ‘like’, $queryText)
->orWhere(‘phone’, ‘like’, $queryText)
怎么同时判断啊?

3年前 评论

user_role 为主表做关联就可以了,这样查出来的用户,就都是带有角色的用户。

3年前 评论
$users = User::with('firm', 'roles')
                ->where('name', 'like', $queryTex)
                ->has('roles')
                ->orWhere('phone', 'like', $queryText)
                ->has('roles')
                ->orderBy('id', 'desc')->paginate(14);

必须这么加俩has么?

3年前 评论
$users = User::with('firm', 'roles')
                ->where(function($query){
                      $query->where('name', 'like', $queryTex)
                         ->orWhere('phone', 'like', $queryText);
                })
                ->has('roles')
                ->orderBy('id', 'desc')->paginate(14);
3年前 评论

这是多对多

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!