Laravel / Eloquent : hasManyThrough WHERE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24388688/
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 09:41:46  来源:igfitidea点击:

Laravel / Eloquent : hasManyThrough WHERE

phplaravelrelational-databaseeloquent

提问by user3518571

In the documentation of Eloquent it is said that I can pass the keys of a desired relationship to hasManyThrough.

Eloquent的文档中,据说我可以将所需关系的键传递给hasManyThrough

Lets say I have Models named Country, User, Post. A Country model might have many Posts through a Users model. That said I simply could call:

假设我有名为 Country、User、Post 的模型。通过用户模型,国家模型可能有许多帖子。也就是说,我可以直接调用:

$this->hasManyThrough('Post', 'User', 'country_id', 'user_id');

This is fine so far! But how can I get these posts only for the user with the id of 3 ?

到目前为止一切正常!但是我怎样才能只为 id 为 3 的用户获取这些帖子?

Can anybody help here?

有人可以帮忙吗?

采纳答案by Jarek Tkaczyk

So here it goes:

所以这里是:

models: Countryhas many Userhas many Post

型号:Country有很多User有很多Post

This allows us to use hasManyThroughlike in your question:

这使我们可以hasManyThrough在您的问题中使用like:

// Country model
public function posts()
{
  return $this->hasManyThrough('Post', 'User', 'country_id', 'user_id');
}

You want to get posts of a given user for this relation, so:

您想获取此关系的给定用户的帖子,因此:

$country = Country::first();
$country->load(['posts' => function ($q) {
  $q->where('user_id', '=', 3);
}]);
// or
$country->load(['posts' => function ($q) {
  $q->has('user', function ($q) {
    $q->where('users.id', '=', 3);
  });
})

$country->posts; // collection of posts related to user with id 3


BUTit will be easier, more readable and more eloquent if you use this instead: (since it has nothing to do with country when you are looking for the posts of user with id 3)

但是如果你使用它,它会更容易,更易读和更有说服力:(因为当你在寻找 id 3 用户的帖子时,它与国家无关)

// User model
public function posts()
{
  return $this->hasMany('Post');
}

// then
$user = User::find(3);
// lazy load
$user->load('posts');
// or use dynamic property
$user->posts; // it will load the posts automatically
// or eager load
$user = User::with('posts')->find(3);

$user->posts; // collection of posts for given user

To sum up: hasManyThroughis a way to get nested relation directly, ie. all the posts for given country, but rather not to search for specific throughmodel.

总结一下:hasManyThrough是一种直接获取嵌套关系的方法,即。给定国家/地区的所有帖子,而不是搜索特定through型号。

回答by Taron Hambardzumyan

$user_id = 3;

$country = Country::find($country_id);

$country->posts()->where('users.id', '=', $user_id)->get();

回答by f_i

$this->hasManyThrough('Post', 'User', 'country_id', 'user_id')->where(column,x);

$this->hasManyThrough('Post', 'User', 'country_id', 'user_id')->where(column,x);

What happen here is you get the collection in return you can put any condition you want at the end.

这里发生的是你得到集合作为回报,你可以在最后放置任何你想要的条件。