Laravel中的急切负载约束筛选器问题 [英] Eager Load Constraints Filter issue in Laravel

查看:57
本文介绍了Laravel中的急切负载约束筛选器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用users表中的username过滤groups表的内容rel ="nofollow">急切的负载约束

I am unable to filter the contents of groups table with respect to username in users table using Eager Load Constraints

public function username()
{
    return $this->belongsTo('User','fk_users_id')->select(['id','username']);
}

我尝试使用下面的代码,但它仅过滤users数据而不过滤groups数据

I have tried using the code below but it filters only the users data not the groups data

$groups     =   Groups::with(array('username' => function($query) use ($keyword)
                                                        {
                                                            $query->where('username', 'like', '%'.$keyword.'%');

                                                        }))
                ->where('status',1)->paginate($paginateValue);

欢迎任何帮助...

推荐答案

认为应该是这样的:

Groups::with('User')->whereHas('User', function($q) use ($key){
 $q->where('username', 'like', '%'.$key.'%');
})->where('status', 1)->paginate($pagVal);

这篇关于Laravel中的急切负载约束筛选器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆