1

我想使用 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"

        },{ ... }
    ],
}
4

4 回答 4

2

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

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"
  ]
]
于 2019-07-16T19:57:30.527 回答
2

使用belongsToMany关系:

在您的Course模型中:

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

然后,使用$course->contents;

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

希望能帮助到你。

于 2019-07-16T20:14:14.107 回答
0

多对多关系是通过contentsCourse模型中的relationship方法中返回一个belongsToMany关系来定义的。如Laravel 多对多文档中所述。

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

于 2019-07-16T19:44:03.240 回答
0

在内容模型中

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();
于 2019-07-16T19:57:16.833 回答