显示第二&第三个模型级

时间:2017-12-21 13:06:40

标签: laravel laravel-5

我知道这是已知的主题,但我没有解决问题。

这是我目前的应用模型设计:

  • 机构是应用程序的父模型,它有许多分支
  • 分支模型与Building模型(child enttity)具有多态关系
  • 课堂模型属于建筑模型(1 Building,M Classroom)

我有一个show.blade.php视图,其中列出了一个分支,我有2个标签,用于建筑物和教室。

我可以展示该分店的每栋建筑,但是当我可以“得到显示该分支中的所有教室(关系是分支 - >建筑 - >教室)

我必须在控制器中使用自定义查询来解决这个问题,然后将结果传递给视图,还是有另一种方法?

我试图向每个机构展示教室......但教室与建筑有关。

 $a=App\Institution::find(1); //OK
 $a->branches; //OK
 $a->buildings; //OK
 $a->buildings->classrooms //Exception with message 'Property [classrooms] does not exist on this collection instance.

此致

机构模型

public function branches()

    {
        return $this->hasMany(Branch::class);
    }

public function buildings()

    {
        return $this->morphMany('App\Building', 'buildingable');
    }

分支模型

public function institution()
    {
        return $this->belongsTo(Institution::class);
    }


public function buildings()

    {
        return $this->morphMany('App\Building', 'buildingable');
    }

构建模型

public function buildingable()

{
    return $this->morphTo();
}


public function classrooms()

    {
        return $this->hasMany(Classroom::class);
    }

课堂模型

public function building()
    {
        return $this->belongsTo(Building::class);
    }

表格结构

 public function up()
    {
        Schema::create('institutions', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('inst_name', 30);
            $table->string('inst_id_type', 10);
            $table->string('inst_id_number', 10);
            $table->string('inst_country', 15);
            $table->string('inst_type', 15);
     });

public function up()
    {
        Schema::create('branches', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('branch_name', 30);
            $table->string('branch_id_type', 10);
            $table->string('branch_id_number', 10);
            $table->string('branch_country', 15);
            $table->string('branch_type', 15);
            $table->integer('institution_id');        
        });
    }

public function up()
    {
        Schema::create('buildings', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('building_name', 30);
            $table->integer('buildingable_id');
            $table->string('buildingable_type');
        });
    }

public function up()
    {
        Schema::create('classrooms', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('classroom_name', 20);
            $table->integer('building_id');
            $table->integer('employee_id'); 
        });
    }

1 个答案:

答案 0 :(得分:0)

$a->buildings->classrooms是问题所在。 $a->buildings是一个集合,而不是一个雄辩的模型。要获得每个建筑物的教室,您可以在连接上使用foreach循环或each()功能。

foreach示例:

foreach($a->buildings as $building) {
    $classrooms = $building->classrooms;
    // Do stuff here...
}

each()示例:

$a->buildings->each(function ($building) {
    $classrooms = $building->classrooms;
    // Do stuff here...
}