Many to Many on same Model ??

Posted 1 month ago by mac03733

assumings its possible. how would i do it

i have a users model , it stores both student and guardian records.i would like to create a many to many relationship( guardian can have many childern "student" and vice verse )

so i tried using a custom pivot model

custom pivot model

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class StudentGuardian extends Pivot
{

    public $incrementing = true;
}

user model


class User extends Authenticatable
{
    use Notifiable;


    public function guardians()
    {
      return $this->belongsToMany('App\User','guardian_user_id')->using('App\StudentGuardian');
    }

    public function children()
    {
      return $this->belongsToMany('App\User','student_user_id')->using('App\StudentGuardian');
    }
}

pivot table schema

  Schema::create('student_guardians', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('student_user_id');
            $table->integer('guardian_user_id');
            $table->string('relationship');
            $table->timestamps();
        });

could this approach be modified to work .. or its not possible

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.