小编Afs*_*adi的帖子

laravel 获取所有具有关系的模型以及这些关系的关系

块有两个关系

\n
\n

1- 与区域一对多

\n

2- 一对多有座位

\n
\n

并且该区域与座位也有一对多的关系

\n
\n

3-区域也与座位有一对多关系

\n
\n

这段代码

\n
 $block=Block::with('regions','seats')->where('id',$blockId)->get();\n
Run Code Online (Sandbox Code Playgroud)\n

将返回此,它不会发送区域关系

\n
0 => Block {#457 \xe2\x96\xbc\n  #relations: array:2 [\xe2\x96\xbc\n    "regions" => Collection {#460 \xe2\x96\xbc\n      #items: array:1 [\xe2\x96\xbc\n        0 => Region {#463 \xe2\x96\xbc\n          #relations: []\n        }\n      ]\n    }\n    "seats" => Collection {#471 \xe2\x96\xb6}\n
Run Code Online (Sandbox Code Playgroud)\n

这里的区域有很多席位,此代码也不会返回区域席位。\ni 只能获取块区域和席位,而区域也有很多席位。

\n

我应该先获取所有地区的 ID,然后尝试获取所有座位吗?,有什么办法吗?

\n

php laravel

6
推荐指数
1
解决办法
1万
查看次数

一般错误:1005 无法创建表,laravel 中的外键约束格式不正确

为什么 Laravel 架构回复

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table test#sql-13cc_d0(errno: 150 "外键约束的格式不正确") (SQL:删除级联上的alter table citiesadd constraintcities_provinces_id_foreign外键( provinces_id) 引用provinces( id))

[PDOException] SQLSTATE[HY000]:一般错误:1005 无法创建表test#sql-13cc_d0(errno: 150 "外键约束的格式不正确")

第一张桌子

Schema::create('provinces', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->boolean('is_enable');
        $table->boolean('is_deletable');
        $table->boolean('is_editable');
        $table->boolean('deleted');
        $table->timestamps();
    }); 
Run Code Online (Sandbox Code Playgroud)

第二张桌子

    Schema::create('cities', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('province_id')->unsigned()->index();
        $table->boolean('is_enable');
        $table->boolean('is_deletable');
        $table->boolean('is_editable');
        $table->boolean('deleted');
        $table->timestamps();


        $table->foreign('province_id')
            ->references('id')->on('provinces')
            ->onDelete('cascade');

    });
Run Code Online (Sandbox Code Playgroud)

laravel laravel-5.2

3
推荐指数
2
解决办法
5438
查看次数

标签 统计

laravel ×2

laravel-5.2 ×1

php ×1