Reputation: 129
I'm trying to add a "simple" relationship into my Route model, but I must have overlooked something because it's not working.
I want my Route to have many steps and the steps should only have one Route, but my code does not find any steps. What did I miss?
routes table:
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();
});
steps table:
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');
});
Route model:
public function steps() {
return $this->hasMany(Step::class, 'route_id', 'id');
}
Step model:
public function route() {
return $this->belongsTo(Route::class);
}
The way I call it in the controller:
$route = Route::all()->where('id', 1)->first();
$steps[] = $route->steps();
Upvotes: 1
Views: 60
Reputation: 4402
You probably want a collection with steps?
But instead you get eloquent steps()
Try and change it for:
$steps[] = $route->steps;
Upvotes: 2