Categories
without category

Pivot table relation in laravel

for implementing many to many relations in laravel you need to define a function in mode :

imagine we have entity user and entity teacher, and of course, we have a pivot table (something like user_teachers table)

let’s implement relation to teacher from user mode :

    public function teachers()
    {
        return $this->belongsToMany(

// the class we want to set a relation -1 
            Teacher::class, 

// pivot table name -2
            'user_teachers', 

// the column refer to the class that we are write code in (user)
            'user_id',


// the column that refer to number 1
            'teacher_id',

// primary key in source table (user)
            'id',

// primary key in target table (techer)
            'id'
        );
    }
Categories
without category

Docker Cheet sheet