【发布时间】:2017-12-08 09:21:38
【问题描述】:
我正在尝试将 3 个模型链接在一起 parent->child->child 的 child。所以我可以在一个雄辩的声明中访问它。
父模型:
class CertificationType extends Model
{
use SoftDeletes;
public $timestamps = true;
protected $table = 'certification_type';
protected $dates = ['deleted_at'];
protected $fillable = array('certification_type_name', 'fa_code', 'icon_image');
protected $hidden = ['created_at', 'updated_at',];
public function certification_type()
{
return $this->hasMany('App\Certification');
}
}
儿童模型
class Certification extends Model
{
protected $table = 'certification';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = array('certification_type_id', 'certification_name', 'certication_date', 'certification_license_number', 'certification_course_length', 'certification_course_description', 'certification_course_topics_list_id');
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'created_at', 'updated_at',
];
public function certification_type()
{
return $this->belongsTo('App\CertificationType',
'certification_type_id');
}
public function certification_course_topics_list()
{
return $this->hasMany('App\CertificationCourseTopicsList');
}
}
Child of Child 模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CertificationCourseTopicsList extends Model
{
protected $table = 'certification_topics_list';
public $timestamps = true;
use SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = array('certification_id', 'certification_topic_description');
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'created_at', 'updated_at',
];
public function certification_course_topics_list()
{
return $this->belongsToMany('App\Certification', 'certification_id');
}
}
我用的雄辩的话是:
$CertificationTypes = App\CertificationType::orderBy('id',"asc")->with('certification_type')->get()->groupBy('id');
我完成的是 CertificationType 在查询中附加了一个 Certifications 的子项。我需要的是第二个孩子也在查询中:
CertificationType::orderBy('id',"asc")->with('certification_type')->get()->groupBy('id')->with('certification')->get() ->groupBy('id');
我也试过了:
CertificationType::orderBy('id',"asc")->with('certification_type, certifications')->get()->groupBy('id');
这不起作用:调用模型 [App\CertificationType] 上的未定义关系 [certification_type,certifications]。
也试过了:
CertificationType::orderBy('id',"asc")->with('certification_type', 'certifications')->get()->groupBy('id');
这不起作用:调用模型 [App\CertificationType] 上的未定义关系 [certifications]。
任何帮助将不胜感激!
【问题讨论】:
-
您的命名确实令人困惑,您的问题需要更简洁。您可以使用点符号获取子模型,例如
Parent::with([ "child", "child.child_of_child"])然后到$parent->child->child_of_child所以填写正确的名字就可以了 -
你是对的,命名应该更好我会清理这个,你的回答很有效,谢谢。 $CertificationTypes = App\CertificationType::orderBy('id',"asc")->with('certification_type', 'certification_type.certification_course_topics_list')->get()->groupBy('id');像魅力一样工作。
-
旁注:
['child', 'child.child_of_child']是多余的...要加载child.child_of_child,它必须先加载child...所以您不需要单独包含child.. 只是child.child_of_child会做