简体   繁体   English

使用laravel 5模型上的多对多关系获取值

[英]get a values using many to many relations on laravel 5 models

i'm really new working with laravel 5.0, so I got this problem when I try to retrieve a result using a model. 我真的是使用laravel 5.0的新手,所以当我尝试使用模型检索结果时遇到了这个问题。 I have a users table, with a list of users who can be a manager or not, they can have assigned one or more companies, or none, a company table with companies which can have one or many managers, and a pivot table that I called companies_managers. 我有一个用户表,其中包含可以成为或不担任经理的用户的列表,他们可以分配一个或多个公司,也可以不分配任何公司,一个可以拥有一个或多个经理的公司的公司表,以及一个数据透视表,称为companies_managers。 I set up the relations in every model like this: 我在每个模型中都建立了这样的关系:

/***User model***/
public function companies()
{
    return $this->belongsToMany('App\Company', 'companies_managers','id', 'manager_id');
}

and the same in Company model 与公司模式相同

public function managers()
{
    return $this->belongsToMany('App\User', 'companies_managers', 'id', 'company_id');
}

I want to get the managers assigned to a company using a company id to get it, but it just gave me an huge object without the values I looking for (the names of the managers assigned to that company). 我想使用公司ID获取分配给公司的经理,但是这给了我一个巨大的对象,没有我要寻找的值(分配给该公司的经理的姓名)。 This is the code that I tried: 这是我尝试的代码:

$managers = Company::find($id)->managers();

I would appreciate any help you can give me 我会很感激你能给我的任何帮助

Using ->managers() (with the brackets) doesn't actually return the associated managers, but rather a Builder instance (the "huge object"), which you can then chain with additional parameters before finally retrieving them with ->get() (or another closure, like ->first() , ->paginate() , etc) 使用->managers() (带有方括号)实际上并不返回关联的管理器,而是返回一个Builder实例(“巨大的对象”),您可以将其与其他参数链接起来,然后再通过->get()检索。 ->get() (或其他闭包,例如->first()->paginate()等)

Using ->managers (without the brackets), will attempt to access the associated managers, and execute any additional logic to retrieve them. 使用->managers (不带括号)将尝试访问关联的管理器,并执行任何其他逻辑来检索它们。

So, you have 2 options: 因此,您有2个选择:

$company = Company::with(["managers"])->findOrFail($id);
$managers = $company->managers;

Or 要么

$company = Company::findOrFail($id);
$managers = $company->managers()->get();

Both of those will perform the necessary logic to pull the managers. 两者都将执行必要的逻辑以吸引管理者。 ->with() and no brackets is slightly more efficient, doing it all in a single query, so bear that in mind. ->with()并且不使用方括号会稍微更有效,在单个查询中完成所有操作,因此请记住这一点。

You just need to split out your code; 您只需要拆分代码即可;

// this will find the company based on the id, or if it cannot find 
// it will fail so will abort the application
$company = Company::findOrFail($id); 


// this uses the active company record and gets the managers based 
// on the current company
$managers = $company->managers;

Thank you for your help guys, I solved the issue fixing the relations in the models to this: 感谢您的帮助,我解决了将模型中的关系固定为此的问题:

        return $this->belongsToMany('App\Company', 'companies_managers', 'manager_id', 'company_id');

and this 和这个

        return $this->belongsToMany('App\User', 'companies_managers', 'company_id', 'manager_id');

The IDs that I had set were not the correct ones for belongsToMany function 我设置的ID不是所属的对正确的函数

And this 和这个

$managers = Company::find($id)->managers();

was a problem too, was a dumb mistake of my part. 这也是一个问题,这是我的愚蠢错误。 I solved the return of Builder instance using just return instead of dd(), in that way I got the values I looking for. 我只使用return而不是dd()解决了Builder实例的返回问题,这样就得到了我想要的值。 Thanks everyone! 感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM