1 year ago

Many to Many on same Model ??

Posted 1 year 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


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) {

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

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