Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 雄辩模型中的拉威尔关系_Php_Database_Laravel - Fatal编程技术网

Php 雄辩模型中的拉威尔关系

Php 雄辩模型中的拉威尔关系,php,database,laravel,Php,Database,Laravel,我试图在我的路线模型中添加一个“简单”的关系,但我肯定忽略了一些东西,因为它不起作用 我希望我的路线有许多步骤,这些步骤应该只有一个路线,但我的代码找不到任何步骤。我错过了什么 路由表: Schema::create('routes', function (Blueprint $table) { $table->increments('id'); $table->string('uuid')->unique(); $ta

我试图在我的路线模型中添加一个“简单”的关系,但我肯定忽略了一些东西,因为它不起作用

我希望我的路线有许多步骤,这些步骤应该只有一个路线,但我的代码找不到任何步骤。我错过了什么

路由表:

     Schema::create('routes', function (Blueprint $table) {
        $table->increments('id');
        $table->string('uuid')->unique();
        $table->decimal('start_lat', 11, 8);
        $table->decimal('start_lng', 11, 8);
        $table->decimal('end_lat', 11, 8);
        $table->decimal('end_lng', 11, 8);
        $table->string('start_address');
        $table->string('end_address');
        $table->integer('distance');
        $table->integer('duration');
        $table->timestamps();
    });
步骤表:

    Schema::create('steps', function (Blueprint $table) {
        $table->increments('id');
        $table->string('uuid')->unique();
        $table->integer('route_id')->unsigned();
        $table->foreign('route_id')->references('id')->on('routes');
        $table->integer('distance');
        $table->integer('duration');
        $table->decimal('start_lat', 11, 8);
        $table->decimal('start_lng', 11, 8);
        $table->decimal('end_lat', 11, 8);
        $table->decimal('end_lng', 11, 8);
        $table->string('instructions');
        $table->text('polyline');
    });
路线模型:

public function steps() {
    return $this->hasMany(Step::class, 'route_id', 'id');
}
步骤模型:

public function route() {
    return $this->belongsTo(Route::class);
}
我在控制器中称之为:

    $route = Route::all()->where('id', 1)->first();
    $steps[] = $route->steps();

您可能想要一个包含步骤的集合

但是你得到的却是雄辩的
steps()

尝试将其更改为:

$steps[] = $route->steps;

什么是
dd(Route::all())给你看?有带那个id的路线吗?它有步骤吗?“它不工作”没有提供很多空间来帮助。对不起,我把问题拼错了。应该是:……但我的代码找不到任何步骤。我确实找到了路线。好的,
dd(Step::all())是什么意思给你看?是否有具有该路由id的步骤?当我返回$route->steps()时,我得到以下错误:类的对象Illumb\Database\Eloquent\Relations\HasMany无法转换为stringWell,这与找不到任何步骤完全不同。不要试图回显
或将其视为字符串。谢谢。问题是它应该是$route->steps;而不是$route->steps();或者对雄辩的关系使用toJSON方法: