R
R
Ruslan Absalyamov2018-03-17 21:06:03
Laravel
Ruslan Absalyamov, 2018-03-17 21:06:03

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.

At first, it popped up when I didn’t put with in the query, now I don’t understand why it pops up
. Here is my implementation of the execute
table

id
last_name
name

direction
id
name

execute_direction
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);

In the model
<?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');
    }
}

In 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');
    }
}

Now I do not understand where to look for a solution to the problem

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question