Laravel Eloquent where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19977818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:39:38  来源:igfitidea点击:

laravel Eloquent where clause

laraveleloquent

提问by

I'm trying to do get all the users from a specific country via Eloquent.

我正在尝试通过 Eloquent 获取来自特定国家/地区的所有用户。

The problem is that I get all the record, the where clause doesn't work.

问题是我得到了所有记录,where 子句不起作用。

$res = User::with(array('country' => function($query) {
          $query->where('country', '=', 'salope');
       }))->get();

Following this pattern from the laravel documentation

遵循 laravel 文档中的这种模式

$users = User::with(array('posts' => function($query)
{
    $query->where('title', 'like', '%first%');
}))->get();

My models :

我的模型:

class User extends SentryUserModel {
    public function country() {

        return $this->belongsTo('country','country_id');
    }
}
class Country extends Eloquent {
    public function users() {

        return $this->hasMany('users');
    }

}

What am I doing wrong ?

我究竟做错了什么 ?

采纳答案by

I found what I was looking for with laravel 4.1 version. My question was not formulated correctly. I wanted to query a relationship.

我在 laravel 4.1 版本中找到了我想要的东西。我的问题没有正确表述。我想查询关系。

$posts = Post::whereHas('comments', function($q)
{
    $q->where('content', 'like', 'foo%');

})->get();

so with my exemple:

所以以我的例子:

 $res = User::whereHas('country', function($q) {
        $q->where('country', 'salope');
       })->get();

Documentation link

文档链接

回答by afarazit

If you want the users for a particular country you need something like that.

如果您想要特定国家/地区的用户,则需要类似的东西。

$users = Country::whereCounty('salope')->users;
dd($users);

Where usersis an eloquent Collectionand you loop over that Collectionand display the users. If you would like to continue building your query based on your users use users()and keep chaining, for example:

users雄辩者在哪里Collection,您可以循环Collection并显示用户。如果您想根据您的用户使用users()并继续链接继续构建您的查询,例如:

$users = Country::whereCounty('salope')->users()->whereBanned(0)->get(); // Or something
dd($users);

回答by user1669496

The problem is you are retrieving all the users and then limiting only the countries those users can have. So you are going to end up with all the users still, and not just the ones that belong to Salope.

问题是您要检索所有用户,然后仅限制这些用户可以拥有的国家/地区。因此,您最终将拥有所有用户,而不仅仅是属于 Salope 的用户。

I think the solution is to go about this backwards.

我认为解决方案是倒退。

$country = Country::where('name', 'salope')->with('users')->first();

foreach($country->users as $user)
{
    echo $user->username;
}

回答by Andino Inyang

$matchThese = ['title' => post('title'), 'slug' => post('slug')];
return Blog::where($matchThese)->get();

Something like this works too

像这样的东西也有效

回答by Tobiushirogeri

This worked for me as of Laravel 5.6

从 Laravel 5.6 开始,这对我有用

I needed to create a relation for users with their roles. The relations has a pivot key using the user_roles tables that is linked to the role table.

我需要为用户及其角色创建关系。关系具有使用链接到角色表的 user_roles 表的主键。

In my User model

在我的用户模型中

public function role(){
        return $this->belongsToMany('App\Roles', 'user_roles','user_id','role_id');
    }

Then, I created a static function in the User model

然后,我在User 模型中创建了一个静态函数

  public static function withRole($role){
        $members = self::whereHas('role',function($q) use($role){
            $q->where('name', '=', $role);
        })->get();
       return $members;
    }

Note the use($role)usage in the function.

注意use($role)函数中的用法。

I can then call this method with :

然后我可以使用以下方法调用此方法:

$members = User::withRole('the_role_name');

Hope this helps someone !

希望这对某人有所帮助!