Laravel ManyToMany多个 [英] Laravel ManyToMany Multiple

查看:157
本文介绍了Laravel ManyToMany多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个模型:UserRoleTool,其中每个用户可以具有许多角色,并且每个角色可以具有许多工具.

在每种情况下,多对多关系都可以正常工作.我可以访问:

User::find(1)->roles
Tool::find(1)->roles
Role::find(1)->tools
Role::find(1)->users

我的表格是:

users
 id
 name

roles
 id
 name

tools
 is
 name

role_user
 id
 role_id
 user_id

role_tool
 id
 role_id
 tool_id

在每个模型中:

//In User Model
public function roles()
{
   return $this->belongsToMany('Rol');
}

//In Role Model
public function users()
{
   return $this->belongsToMany('User');
}

public function tools()
{
   return $this->belongsToMany('Tool');
}

//In Tool Model
public function roles()
{
   return $this->belongsToMany('Rol');
}

我需要获得单个用户的所有工具,例如:User::find(1)->roles()->tools.我该怎么办?

解决方案

获取用户的所有角色,然后在一个循环中获得该角色的所有工具并将它们合并到存储所有工具的数组中.

>

$tools = array();
foreach(User::find(1)->roles as $role)
    $tools = array_merge($tools, $role->tools->toArray());

这会为每个迭代运行一个查询,因此为了获得更好的性能,您应该使用快速加载.

$tools = array();
foreach (User::find(1)->load('roles.tools')->roles as $role) {
    $tools = array_merge($tools, $role->tools->toArray());
}

现在,您可以将其放置在User模型中的名为tools()的函数中.

public function tools()
{
    $tools = array();   
    foreach ($this->load('roles.tools')->roles as $role) {
        $tools = array_merge($tools, $role->tools->toArray());
    }
    return $tools;
}

您可以这样称呼它:User::find(1)->tools().

我认为该框架没有更好的解决方案.另一种方法是使用Fluent Query Builder并创建自己的查询,但我不认为这会更好.

I have 3 models: User, Role, Tool where each user could have many roles, and each role could have many tools.

The many to many relationships work well in each case. I can access:

User::find(1)->roles
Tool::find(1)->roles
Role::find(1)->tools
Role::find(1)->users

My tables are:

users
 id
 name

roles
 id
 name

tools
 is
 name

role_user
 id
 role_id
 user_id

role_tool
 id
 role_id
 tool_id

In each model:

//In User Model
public function roles()
{
   return $this->belongsToMany('Rol');
}

//In Role Model
public function users()
{
   return $this->belongsToMany('User');
}

public function tools()
{
   return $this->belongsToMany('Tool');
}

//In Tool Model
public function roles()
{
   return $this->belongsToMany('Rol');
}

I need to get all the tools of a single user like: User::find(1)->roles()->tools. How can I do that?

解决方案

Get all the roles of the user, then in a loop you get all tools of the role and merge them to an array where you store all tools.

$tools = array();
foreach(User::find(1)->roles as $role)
    $tools = array_merge($tools, $role->tools->toArray());

This runs a query for every iteration, so for better performance you should use eager loading.

$tools = array();
foreach (User::find(1)->load('roles.tools')->roles as $role) {
    $tools = array_merge($tools, $role->tools->toArray());
}

Now you can place this to a function called tools() in your User model.

public function tools()
{
    $tools = array();   
    foreach ($this->load('roles.tools')->roles as $role) {
        $tools = array_merge($tools, $role->tools->toArray());
    }
    return $tools;
}

You can call it like this: User::find(1)->tools().

I don't think that the framework has a better solution. One other method is to use the Fluent Query Builder and create your own query but I don't see how that would be better.

这篇关于Laravel ManyToMany多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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