I have this scenario in my app where I have to limit post that can be written by users. Example, I have a table named Edition to store edition data and Article table to store article data. Edition table and Article table has one-to-many relationship (one Edition has many Articles). Now, I have a Users table to store users data and this table has one-to-many relationship with Article table (one User has many Articles). Cut to the case, I want to make a rule where a user CAN ONLY HAVE ONE ARTICLE IN EACH EDITION. Let's just say there is an edition record called "Japanese Food", in this edition record users may post their article but ONLY ONE ARTICLE. Users that already post their article in this "Japanese Food" edition will only be able to post a new article in another edition record where they haven't post any article yet. Now my question is, how to make such rule in Laravel ? especially Laravel 5.3. I have created my migrations and the models, below are the codes (in case it's necessary to help you answer my question).
Edition table :
public function up() {
Schema::create('edition', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('volume');
$table->text('cover')->nullable();
$table->integer('number');
$table->timestamps();
});
Schema::table('article', function(Blueprint $table) {
$table->foreign('id_edition')->references('id')->on('edition')->onDelete('cascade')->onUpdate('cascade');
});
}
public function down() {
Schema::table('article', function(Blueprint $table) {
$table->dropForeign('article_id_edition_foreign');
});
Schema::drop('edition');
}
Article table :
public function up() {
Schema::create('article', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->text('content');
$table->text('file');
$table->integer('id_edition')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('articleslug');
$table->enum('publish', ['yes', 'no']);
$table->timestamps();
});
Schema::table('article', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}
public function down() {
Schema::table('article', function(Blueprint $table) {
$table->dropForeign('article_user_id_foreign');
});
Schema::drop('article');
}
Users table :
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username')->unique();
$table->string('userslug');
$table->string('nameslug');
$table->string('email')->unique();
$table->string('phone')->nullable();
$table->string('address')->nullable();
$table->string('password');
$table->rememberToken();
$table->enum('level', ['admin', 'author']);
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
Thank you for the helps! :)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire