I have a migration with three tables and i have a question which decide the how the tables will connect to each other and do i really a foreign key on them so what i want to do I have two roles 1 admin - 2 user and i want to assign one of them to each user because i want only admin users to login into backend dashboard as for now i make the relationship between roles and users as one(role)-to-many(users) Also the relationship between posts and users is one(user)-to-many(posts) since the post in only related to one user only but the user can have multiple posts so i need you help here to understand those thing in better way and when i need to setup a Foreign Key in the table.
This is my tables:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fullname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles');
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('content');
$table->string('image');
$table->timestamps();
});
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire