【问题标题】:Eloquent Relationships Laravel connecting multiple tables together雄辩的关系 Laravel 将多个表连接在一起
【发布时间】:2020-05-08 16:19:33
【问题描述】:

我正在尝试将我的所有联接重写为 Elequent 模型关系。 这是我目前所拥有的:

class SectionAndUser
{
    public function sections()
    {        
        return $this->belongsTo('App\Models\Section');
    }

    public function users()
    {
        return $this->belongsTo('App\Models\User');
    }
    ...
class User
{
    public function sectionAndUser()
    {
        return $this->hasMany('App\Models\SectionAndUser');
    }
    ...
class Section
{
    public function sectionAndUsers()
    {
        return $this->hasMany('App\Models\SectionAndUser');
    }
    ...

使用选择:

 $sections = User::find($userId)->sectionAndUser()->get();

我得到结果:

{
    "id": 1,
    "section_id": 1,
    "user_id": 133
}

我现在如何附加包含关于 section_id 1 的所有数据的 3 模型部分?

这是我希望实现的连接:

$id=Auth::id();
$results = DB::table('sections')
    ->join('section_and_users', function ($join) use ($id) {
        $join->on('sections.id', '=', 'section_and_users.section_id')
            ->where('section_and_users.user_id','=', $id);
    })
    ->get();

预期结果:

{
    "id": 1,
    "section_id": 1,
    "section_name": 'sectionName'
    "user_id": 133
}

【问题讨论】:

  • 嗨,您为什么要使用 3 个模型而不是两个模型?
  • 由于是多对多关系,用户可以属于多个section,section可以有多个用户,所以我做了一个链接表

标签: php laravel eloquent


【解决方案1】:

你可以这样做

$id=Auth::id();
$results = SectionAndUser::where('user_id', $id)->with('users', 'sections')->get();

然后你可以映射它以获得你想要的输出

$sections = collect($results)->map(function ($section){
     return [
          'id' => $section->id,
          'section_id' => $section->id,
          'section_name' => $section->sections->name,
          'user_id' => $section->user_id
     ];

});

【讨论】:

    【解决方案2】:

    我认为解决方案是只创建模型SectionUser,并将关系添加为 BelongsToMany。

    class User
    {
        public function sections()
        {
            return $this->BelongsToMany('App\Models\Section');
        }
        ...
    

    <?
    
    class Section
    {
        public function users()
        {
            return $this->BelongsToMany('App\Models\User');
        }
        ...
    

    当然,您还需要创建数据透视表。可以咨询BelongsToMany documentation

    如果你使用这种方式,你可以通过这个查询简单地得到结果:

    $section = Section::find(1); // This will return all your Section data
    $section_related_users = $section->users; // This will return a collection of Users
    

    【讨论】:

      【解决方案3】:

      您可以在没有SectionAndUser-model 的情况下创建多对多关系。

      使用belongsToMany-方法,您可以将数据透视表的名称作为第二个参数传递。如果您想知道可以传递哪些其他参数,可以查看Illuminate\Database\Eloquent\Concerns\HasRelationships@belongsToMany

      部分:

      class Section extends Model
      {
          ...
          public function users()
          {
              return $this->belongsToMany(User::class, 'section_and_users');
          }
          ...
      }
      

      用户:

      class User extends Model
      {
          ...
          public function sections()
          {
              return $this->belongsToMany(Section::class, 'section_and_users');
          }
          ...
      }
      

      然后像这样使用它: $user-&gt;sections-&gt;where(...

      【讨论】:

        【解决方案4】:
        // Post Model
         public function user()
            {
                return $this->belongsTo('App\User');
            }
        
            public function categories()
            {
                return $this->belongsToMany('App\Category')->withTimestamps();
            }
        
            public function tags()
            {
                return $this->belongsToMany('App\Tag')->withTimestamps();
            }
        // Role Model
         public function users()
            {
        
                return $this->hasMany('App\User');
        
            }
        
        // User Model
         public function role()
            {
              return $this->belongsTo('App\Role');
            }
        
            public function posts()
            {
                return $this->hasMany('App\Post');
            }
        //Tag Model
        public function posts()
            {
                return $this->belongsToMany('App\Post')->withTimestamps();
            }
        
        // Catgory Model
         public function posts()
            {
                return $this->belongsToMany('App\Post')->withTimestamps();
            }
        // controller 
         public function index()
            {
                $posts = Post::latest()->get();
                return view('admin.post.index',compact('posts'));
            }
        // posts tabel to user result get
         foreach($posts as $post){
        $post->user->name
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-17
          • 2017-07-26
          • 2012-10-08
          • 2018-06-02
          • 2018-10-27
          • 2019-06-09
          • 2020-05-30
          • 1970-01-01
          相关资源
          最近更新 更多