laravel 如何在 Eloquent 上设置条件关系

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

How to setup conditional relationship on Eloquent

laraveleloquentrelationships

提问by Ju Nogueira

I have this (simplified) table structure:

我有这个(简化的)表结构:

users
- id
- type (institutions or agents)

institutions_profile
- id
- user_id
- name

agents_profile
- id
- user_id
- name

And I need to create a profilerelationship on the Usersmodel, but the following doesn't work:

我需要profileUsers模型上创建关系,但以下不起作用:

class User extends Model
{
    public function profile()
    {
        if ($this->$type === 'agents')
            return $this->hasOne('AgentProfile');
        else
            return $this->hasOne('InstitutionProfile');
    }    
}

How could I achieve something like that?

我怎么能实现这样的目标?

回答by oseintow

Lets take a different approach in solving your problem. First lets setup relationship for the various models respectively.

让我们采用不同的方法来解决您的问题。首先让我们分别为各种模型设置关系。

class User extends Model
{
    public function agentProfile()
    {
        return $this->hasOne(AgentProfile::class);
    }    

    public function institutionProfile()
    {
        return $this->hasOne(InstitutionProfile::class);
    }

    public function schoolProfile()
    {
        return $this->hasOne(SchoolProfile::class);
    }

    public function academyProfile()
    {
        return $this->hasOne(AcademyProfile::class);
    }

    // create scope to select the profile that you want
    // you can even pass the type as a second argument to the 
    // scope if you want
    public function scopeProfile($query)
    {
        return $query
              ->when($this->type === 'agents',function($q){
                  return $q->with('agentProfile');
             })
             ->when($this->type === 'school',function($q){
                  return $q->with('schoolProfile');
             })
             ->when($this->type === 'academy',function($q){
                  return $q->with('academyProfile');
             },function($q){
                 return $q->with('institutionProfile');
             });
    }
}

Now you can access your profile like this

现在您可以像这样访问您的个人资料

User::profile()->first();

This should give you the right profile. Hope it helps.

这应该为您提供正确的配置文件。希望能帮助到你。

回答by Mortada Jafar

you can do this by use another method please check this:

您可以通过使用另一种方法来做到这一点,请检查:

a blog Post and Video model could share a polymorphic relation to a Tag model. Using a many-to-many polymorphic relation allows you to have a single list of unique tags that are shared across blog posts and videos. First, let's examine the table structure:

博客 Post 和 Video 模型可以与 Tag 模型共享多态关系。使用多对多多态关系允许您拥有一个在博客文章和视频之间共享的唯一标签列表。首先,让我们检查一下表结构:

https://laravel.com/docs/5.4/eloquent-relationships#many-to-many-polymorphic-relations

https://laravel.com/docs/5.4/eloquent-relationships#many-to-many-polymorphic-relations

回答by Joel Hinz

Looks like that should be $this->typerather than $this->$type- since typeis a property, not a variable.

看起来应该是$this->type而不是$this->$type- 因为它type是一个属性,而不是一个变量。