对象中的多关系Laravel-对象
我想使用Laravel Eloquent来处理关系,但在访问关系中的特定过滤对象时遇到问题 我的目标:对象中的多关系Laravel-对象,laravel,eloquent-relationship,Laravel,Eloquent Relationship,我想使用Laravel Eloquent来处理关系,但在访问关系中的特定过滤对象时遇到问题 我的目标: courses: id - integer name - string contents: id - integer name - string course_contents: id - integer course_id - integer content_id - integer 我想通过课程获得内容。到目前为止,我只能过滤课程内容以过滤内容 我的控
courses:
id - integer
name - string
contents:
id - integer
name - string
course_contents:
id - integer
course_id - integer
content_id - integer
我想通过课程获得内容。到目前为止,我只能过滤课程内容以过滤内容
我的控制器:
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"
},{ ... }
],
}
多对多关系是通过在课程模型的关系方法内容中返回归属关系来定义的。如合同中所述
要仅检索多对多关系中的内容项,而不检索透视列,应将关系实例从App\CourseContent更改为App\Content。多对多关系是通过在课程模型的关系方法内容中返回一个belongTomany关系来定义的。如合同中所述 要仅检索多对多关系中的内容项,而不检索透视列,应将关系实例从App\CourseContent更改为App\Content。在内容模型中
public function course()
{
return $this->hasMany('App\Course');
}
课程内模式
public function content()
{
return $this->hasMany('App\Content');
}
你能行
Course::with('content')->find($id)
或
内容内模型
public function course()
{
return $this->hasMany('App\Course');
}
课程内模式
public function content()
{
return $this->hasMany('App\Content');
}
你能行
Course::with('content')->find($id)
或
首先,你需要稍微调整一下关系。您有多对多关系,因此模型应如下所示: Course.php 公共职能内容 { 返回$this->belongTomanyContent::class,“课程内容”; } Content.php 受保护的$hidden=['pivot']; 公共职能课程 { 返回$this->belongTomanyCourse::class,“课程内容”; } 您可以检索内容数据,如下所示: 例如:您希望获取课程1的所有内容 内容::whereHas'courses',函数$query{ $query->where'courses.id',1; }->得到; //你们需要动态地通过课程id,但为了演示,我硬编码了它。 这将为您提供以下结果:
array:1 [
0 => array:2 [
"id" => 1
"name" => "Content 1"
]
]
首先,你需要稍微调整一下关系。您有多对多关系,因此模型应如下所示: Course.php 公共职能内容 { 返回$this->belongTomanyContent::class,“课程内容”; } Content.php 受保护的$hidden=['pivot']; 公共职能课程 { 返回$this->belongTomanyCourse::class,“课程内容”; } 您可以检索内容数据,如下所示: 例如:您希望获取课程1的所有内容 内容::whereHas'courses',函数$query{ $query->where'courses.id',1; }->得到; //你们需要动态地通过课程id,但为了演示,我硬编码了它。 这将为您提供以下结果:
array:1 [
0 => array:2 [
"id" => 1
"name" => "Content 1"
]
]
使用以下关系:
在您的课程模式中:
公共职能内容
{
返回$this->belongTomanyContents::class,“课程内容”;
}
然后,使用$course->contents
此函数用于返回课程的所有内容模型
希望有帮助。使用以下关系:
在您的课程模式中:
公共职能内容
{
返回$this->belongTomanyContents::class,“课程内容”;
}
然后,使用$course->contents
此函数用于返回课程的所有内容模型
希望能有所帮助。我想你没有正确理解我的问题,我更新了回答以进一步澄清。明白了,这样你就可以在内容模型中隐藏透视表信息。我更新了我的回答我认为你没有正确理解我的问题,我更新了回答以进一步澄清。明白了,所以你可以在内容模型中隐藏透视表信息。我更新了我的答案