【问题标题】:How to chain 3 laravel nested models如何链接 3 个 laravel 嵌套模型
【发布时间】: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-&gt;child-&gt;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 会做

标签: php laravel eloquent


【解决方案1】:

您需要更多的代码顺序。我会制作三个具有适当关系的模型: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多