【问题标题】:Show second & third model level显示第二和第三模型级别
【发布时间】:2018-06-04 04:09:26
【问题描述】:

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

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

  • Institution 是应用的父模型,有很多分支
  • 分支模型与建筑模型(子实体)具有多态关系
  • 教室模型属于建筑模型(1栋,M教室)

我有一个 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'); 
        });
    }

【问题讨论】:

  • 你能提供一些表结构吗?关系似乎有点混乱(对我来说)!
  • 是的,确定@ab_in 我会更新帖子
  • 你尝试过什么?你哪里出错了?
  • @ab_in ,我试图展示每个机构的教室......但教室与建筑物有关 >>> $a=App\Institution::find(1); // OK >>> $a->branchs; // OK >>> $a->buildings; // OK >>> $a->buildings->classrooms 异常,带有消息“属性 [classrooms] 在此集合实例上不存在。”
  • 您的$a->buildings 返回什么?不止一个记录?您是否循环了结果并在循环内使用了$building->classrooms

标签: laravel laravel-5


【解决方案1】:

$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...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 2023-01-07
    • 1970-01-01
    • 2021-06-30
    • 2015-07-11
    • 1970-01-01
    相关资源
    最近更新 更多