vendredi 22 février 2019

Laravel Non Primary key to be a foreign key in different Table Role_id cannot be a foreign key from user table to Role Table

This is mine User Model

public function role()
    {
        return $this->belongsToMany('App\Role');
    }

This is mine Role Model

public function users(){
        return $this->hasMany('App\User');
    }

This is mine Migration Files User Migration file

$table->increments('id');
            $table->integer('role_id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('image')->nullable();
            $table->rememberToken();
            $table->timestamps();

This is mine Roles Migration File

$table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->foreign('role_id')
            ->references('role_id')
            ->on('users')
            ->onDelete('cascade');
            $table->timestamps();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire