Eager Load Constraints Filter issue in Laravel

本小妞迷上赌 提交于 2019-12-01 09:15:06

问题


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

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

any help is welcome...


回答1:


Think it should be something like this:

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


来源:https://stackoverflow.com/questions/27266086/eager-load-constraints-filter-issue-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!