Answer the question
In order to leave comments, you need to log in
How to make a many-to-many relationship?
I don't understand how the many-to-many relationship is implemented. I'm trying to figure it out, but I still don't fully understand why I'm getting this error.
Property [executes] does not exist on this collection instance.
id
last_name
name
id
name
id
direction_id
execute_id
In the controller$directions = Direction::where('id', 33)->with('executes')->get();
$execute = [];
foreach ($directions->executes as $value){
$execute[] = $value;
}
dd($execute);
<?php
namespace Growth;
use Illuminate\Database\Eloquent\Model;
class Direction extends Model
{
protected $table = 'direction';
protected $fillable = ['id_user', 'name'];
public function executes()
{
return $this->belongsToMany('Growth\Execute', 'execute_direction');
}
}
<?php
namespace Growth;
use Illuminate\Database\Eloquent\Model;
class Execute_direction extends Model
{
protected $table = 'execute_direction';
protected $fillable = ['direction_id', 'execute_id'];
public $timestamps = false;
public function execute()
{
return $this->hasOne('Growth\Execute');
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question