如何链接3个laravel嵌套模型

时间:2017-12-08 09:21:38

标签: php laravel eloquent

我正在尝试将3个模型链接在一起父母>孩子 - >孩子的孩子。所以我可以用雄辩的声明来访问它。

父模型:

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');
    }


}

儿童模型儿童

<?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在查询中附加了一个认证子项。我需要的是第二个孩子也在查询中:

CertificationType :: ORDERBY( 'ID', “ASC”) - &gt;在( 'certification_type') - &GT;得到() - &GT; GROUPBY( '编号') - &gt;在( '认证') - &GT;得到() - &GT; 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]上调用未定义的关系[证书]。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要更多代码订单。我将制作三个具有适当关系的模型:belongsTo和hasMany。然后在每个视图中,您可以使用具有关系函数的模型中的数据。

关系正确模型的示例:     

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

在此处阅读更多内容,这将节省您的时间:https://laravel.com/docs/5.5/eloquent-relationships#one-to-many