【问题标题】:Multiple Relationship Laravel - Object in Object多重关系 Laravel - 对象中的对象
【发布时间】:2019-07-16 19:24:36
【问题描述】:

我想使用 Laravel Eloquent 建立关系,但我在访问关系中的特定过滤对象时遇到问题。

我的对象:

courses:  
  id - integer
  name - string

contents:
  id - integer
  name - string

course_contents:
  id - integer
  course_id - integer
  content_id - integer

我想按课程获取内容。到目前为止我只能过滤 course_contents 来过滤内容

我的控制器:

Course::hasContents()->find($id);

课程模式

public function contents()
    {
        return $this->hasMany('App\CourseContent');
    }

    public function scopeHasContents($query)
    {
        $query->with(['contents' => function($contentQuery) {
            $contentQuery->has('content')->with('content');
        }]);
    }

课程内容模型:

public function content()
    {
       return $this->hasOne('App\Content', 'id');
    }

我的 json 返回(课程查找):

{  
    "id":1,
    "name":"Course Example 1",
    "contents":[  
        {  
            "id":1,
            "course_id":1,
            "content_id":1,
            "deleted_at":null,
            "created_at":"2019-07-16 17:31:05",
            "updated_at":"2019-07-16 17:31:05",
            "content":{  
                "id":1,
                "name":"Content Example 1",
                "deleted_at":null,
                "created_at":"2019-07-16 17:31:05",
                "updated_at":"2019-07-16 17:31:05"
            }
        },
        {  
            "id":2,
            "course_id":1,
            "content_id":2,
            "deleted_at":null,
            "created_at":"2019-07-16 17:31:05",
            "updated_at":"2019-07-16 17:31:05",
            "content":{  
                "id":2,
                "name":"Content Example 2",
                "deleted_at":null,
                "created_at":"2019-07-16 17:31:05",
                "updated_at":"2019-07-16 17:31:05"
            }
        },{ ... }
    ],
}

我需要什么:

{  
    "id":1,
    "name":"Course Example 1",
    "contents":[  
        {  
            "id":1,
            "name":"Content Example 1",
            "deleted_at":null,
            "created_at":"2019-07-16 17:31:05",
            "updated_at":"2019-07-16 17:31:05"

        },
        {  
            "id":2,
            "name":"Content Example 2",
            "deleted_at":null,
            "created_at":"2019-07-16 17:31:05",
            "updated_at":"2019-07-16 17:31:05"

        },{ ... }
    ],
}

【问题讨论】:

    标签: laravel eloquent-relationship


    【解决方案1】:

    首先,您需要稍微调整一下关系。您有多对多的关系,因此模型应如下所示:

    Course.php

    public function contents()
    {
        return $this->belongsToMany(Content::class, 'course_contents');
    }
    


    Content.php

    protected $hidden = ['pivot'];
    
    public function courses()
    {
        return $this->belongsToMany(Course::class, 'course_contents');
    }
    


    您可以检索contents 数据,如下所示: 例如:您想获取课程的所有内容1

    Content::whereHas('courses', function($query) {
        $query->where('courses.id', 1);
    })->get();
    
    // You need to pass course id dynamically but for demonstration, I hard coded it.
    

    这将为您提供以下结果:

    array:1 [
      0 => array:2 [
        "id" => 1
        "name" => "Content 1"
      ]
    ]
    

    【讨论】:

    • 我认为你没有正确理解我的问题,我更新了回复以澄清一点。
    • 知道了,所以你可以在Content模型中隐藏pivot表信息。我更新了我的答案
    【解决方案2】:

    使用belongsToMany 关系:

    在您的Course 模型中:

    public function contents()
    {
        return $this->belongsToMany(Contents::class, 'course_contents');
    }
    

    然后,使用$course->contents;

    此函数返回课程的所有内容模型。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      多对多关系是通过在Course 模型中的关系方法contents 中返回一个belongsToMany 关系来定义的。如Laravel many-to-many documentation中所述。

      要仅检索多对多关系中的 Content 项而不是数据透视列,您应该将关系实例从 App\CourseContent 更改为 App\Content

      【讨论】:

        【解决方案4】:

        在内容模型中

        public function course()
            {
                return $this->hasMany('App\Course');
            }
        

        在课程模型中

        public function content()
            {
                return $this->hasMany('App\Content');
            }
        

        你可以的

        Course::with('content')->find($id)
        

        Content::whereHas('course',function($q)use($id){
        $q->where('id',$id)
        })->get();
        

        【讨论】:

          猜你喜欢
          • 2010-10-21
          • 1970-01-01
          • 1970-01-01
          • 2010-12-07
          • 1970-01-01
          • 1970-01-01
          • 2020-12-17
          • 2012-04-18
          相关资源
          最近更新 更多