检查 Laravel 是否存在多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20071243/
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
Check whether many-to-many relationship exists Laravel
提问by Mike F
I have the following Models:
我有以下型号:
/* Team.php */
class Team extends Eloquent {
public function users() {
return $this->belongsToMany('User');
}
}
/* User.php */
class User extends Eloquent {
public function teams() {
return $this->hasMany('Team');
}
}
and database structure:
和数据库结构:
teams
id
user_id
name
users
id
email
team_user
id
team_id
user_id
A user can create (and own - hence teams.user_id) a team, but also belong to other teams.
用户可以创建(并拥有 - 因此teams.user_id)一个团队,但也可以属于其他团队。
If possible, I'd like to be able to get all the teams for a particular user using Eloquent without doing a custom SQL query like so:
如果可能,我希望能够使用 Eloquent 获取特定用户的所有团队,而无需执行如下自定义 SQL 查询:
SELECT teams.*
FROM teams
JOIN team_user
ON (team_user.team_id = teams.id)
WHERE team_user.user_id = ?
The custom SQL query works but I'm convinced this isn't the native way to go about this in Laravel.
自定义 SQL 查询有效,但我确信这不是 Laravel 中处理此问题的本机方式。
Can anyone help? Thanks in advance for reading.
任何人都可以帮忙吗?提前感谢您的阅读。
采纳答案by David Barker
You should be returning $this-belongsToMany(Model::class)
on both sides of the relationship for your pivot table to be used.
您应该返回$this-belongsToMany(Model::class)
关系的双方以使用您的数据透视表。
/* Team.php */
class Team extends Eloquent {
public function users() {
return $this->belongsToMany(User::class);
}
}
/* User.php */
class User extends Eloquent {
public function teams() {
return $this->belongsToMany(Team::class);
}
}
There is no need to have a user_id
field in teams as this implicitly says Many-to-one
. (This team has one user only)
user_id
正如这暗示的那样,团队中不需要有一个领域Many-to-one
。(这个团队只有一个用户)
With this fixed up, using eloquent to get all teams for a user would be as simple as:
解决此问题后,使用 eloquent 为用户获取所有团队将非常简单:
User::teams()->get();
And the inverse
而相反
Team::users()->get();
Or to get a user entity with all their teams:
或者获取所有团队的用户实体:
User::with('teams')->get();
And again vice versa
反之亦然
Team::with('users')->get();
回答by sbkl
To answer to the question in the title, here is how you define a boolean function in the user model to know if the relation exists in the pivot table 'team_user':
要回答标题中的问题,以下是您如何在用户模型中定义布尔函数以了解该关系是否存在于数据透视表“team_user”中:
public function isPartOfThe($team)
{
return $this->teams()->where('team_id', $team->id)->exists();
}
Then you can call the function in your controller as follow:
然后你可以在你的控制器中调用函数,如下所示:
If($user->isPartOfThe($team)
{
//true
}
回答by joshbrw
Should be something like this
应该是这样的
class User {
public function teams() {
return $this->belongsToMany('User', 'team_user');
}
}
class Team {
public function user() {
return $this->belongsToMany('Team', 'team_user');
}
}
回答by Abba Bryant
There is no reason to get rid of the user_id column on teams if you are using it to determine ownership of that object.
如果您使用它来确定该对象的所有权,则没有理由摆脱团队中的 user_id 列。
That being said, naming the key more appropriately to manager_id or something similar would make more sense. Also, nothing dicates that you have to use a relationship function name that is the same as the related (pluralized) table name.
话虽如此,将密钥更恰当地命名为 manager_id 或类似的东西会更有意义。此外,没有任何迹象表明您必须使用与相关(复数)表名称相同的关系函数名称。
Try something like this in addition to the belongsToMany setup above:
除了上面的belongsToMany 设置之外,还可以尝试这样的操作:
<?php
class Team extends Eloquent {
...
public function manager()
{
return belongsTo('User', 'manager_id');
}
...
}
<?php
class User extends Eloquent {
...
public function manages()
{
return hasMany('Team', 'manager_id');
}
...
}
I don't feel like the previous answers accounted for the data model you were trying for completely.
我不觉得之前的答案完全说明了您正在尝试的数据模型。
回答by Antonio Carlos Ribeiro
Looks to me that you are almost there, you already have your relations set so now you just need to:
在我看来你就快到了,你已经建立了你的关系,所以现在你只需要:
$user = User::find(1);
foreach($user->teams as $team)
{
echo $team->name;
}
Since Laravel will expect a teams_users pivot table name, you might also need to tell it the correct one and use belongsToMany() on both:
由于 Laravel 需要一个 team_users 数据透视表名称,因此您可能还需要告诉它正确的名称并在两者上使用belongsToMany():
class Team extends Eloquent {
public function users() {
return $this->belongsToMany('User', 'team_user');
}
}
class User extends Eloquent {
public function teams() {
return $this->belongsToMany('Team', 'team_user');
}
}