dongxi4235
2017-02-03 22:07
浏览 81
已采纳

Laravel有一个通过数据透视表

So I have 2 models, User & Profile, the relationship is set up as follows:

    /**
     * User belongs to many Profile
     *
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */
    public function profiles()
    {
        return $this->belongsToMany('App\Models\Profile', 'user_profiles');
    }

I have 3 tables, users, profiles & user_profiles (the pivot table)

I have a column in my users table called active_profile which gets populated with a profile id.

How can I set up the relationship so that I can call something like the following:

$user->active_profile

So that it will return all the profile information for the id set in active_profile?

图片转代码服务由CSDN问答提供 功能建议

所以我有2个模型,User& 配置文件,关系设置如下:

  / ** 
 *用户属于许多配置文件
 * 
 * @return \ Illuminate \ Database \ Eloquent \  Relations \ belongsToMany 
 * / 
公共功能配置文件()
 {
返回$ this-> belongsToMany('App \ Models \ Profile','user_profiles'); 
} 
  <  / pre> 
 
 

我有3个表,用户,个人资料和 user_profiles(数据透视表)

我的users表中有一个名为 active_profile 的列,其中填充了配置文件ID。

如何设置关系以便我可以调用以下内容:

$ user-&gt; active_profile

这样它将返回active_profile中设置的id的所有配置文件信息?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duananyantan04633 2017-02-03 22:29
    已采纳

    You can add a method into your User model like this :

    public function active_profile ()
    {
       return $this->profiles()
              ->find($this->active_profile);
    }
    

    and then you can call the method as $user->active_profile();

    已采纳该答案
    打赏 评论
  • douzhe9075 2017-02-03 22:11

    You should be able to grab the resource using find

    $user->profiles()->find($user->active_profile);
    
    打赏 评论
  • dongzhun8449 2017-02-05 10:09

    I could be wrong but why not just use a belongsTo relationship?

    public function active_profile()
    {
        return $this->belongsTo('App\Models\Profile', 'active_profile');
    }
    

    Hope this helps!

    打赏 评论
  • dtxb75622 2018-07-30 14:40

    You could add extra field in pivot table, for example active = enum(y, n), then set unique key (user_id, active) and then use wherePivotmethod in user model:

    public function profiles()
    {
      return $this->belongsToMany('App\Models\Profile', 'user_profiles');
    }
    
    public function activeProfile()
    {
      return $this->belongsToMany('App\Models\Profile', 'user_profiles')->wherePivot('active', 'y');
    }
    

    It requires DB refactoring though.

    打赏 评论
  • dougu3290 2019-07-01 08:36

    On Laravel 5.8, since I wanted to use it with eager loading I used this package: https://github.com/staudenmeir/eloquent-has-many-deep

    This was my scenario
    A user can be tagged on many photo and a photo can have many users tagged on. I wanted to have a relationship to get the latest photo the user has been tagged on.

    I think my scenario can also be applied to any many-to-many relationship

    I did the pivot model UserPhoto

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Relations\Pivot;
    
    class UserPhoto extends Pivot
    {
    
        public function user()
        {
            return $this->belongsTo(User::class);
        }
    
        public function photo()
        {
            return $this->belongsTo(Photo::class);
        }
    
    }
    

    And then on my User model using staudenmeir's package:

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Staudenmeir\EloquentHasManyDeep\HasRelationships;
    
    class User extends Model
    {
        use HasRelationships;
    
        public function photos()
        {
            return $this->belongsToMany(Photo::class);
        }
    
        public function latestPhoto()
        {
            return $this->hasOneDeep(Photo::class, [UserPhoto::class])
                ->latest();
        }
    }
    

    Then I can easily do something like this:

    User::with('latestPhoto')->get() and $user->latestPhoto

    打赏 评论

相关推荐 更多相似问题