I have this scenario in my app, a user can only upload one journal in each edition (an edition contains many journals from many users) but a user can also tag another users to entitle them as one of the journal authors if needed (tagged user will still be able to upload a journal to the same edition with a journal where the user is tagged in as the author if the user haven't upload any journal yet). So far I managed to make this happen except for limiting the number of files that can be uploaded in each edition. Below are my migrations :
Edition table :
class CreateTableEdition extends Migration {
public function up() {
Schema::create('edition', function (Blueprint $table) {
$table->increments('id');
$table->integer('volume');
$table->text('cover')->nullable();
$table->integer('number');
$table->timestamps();
});
Schema::table('journal', function(Blueprint $table) {
$table->foreign('id_edition')->references('id')->on('edition')->onDelete('cascade')->onUpdate('cascade');
});
}
public function down() {
Schema::table('journal', function(Blueprint $table) {
$table->dropForeign('journal_id_edition_foreign');
});
Schema::drop('edition');
}
}
Journal table :
class CreateTableJournal extends Migration {
public function up() {
Schema::create('journal', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->text('abstract');
$table->text('file');
$table->integer('id_edition')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('journalslug');
$table->timestamps();
});
Schema::table('journal', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}
public function down() {
Schema::table('journal', function(Blueprint $table) {
$table->dropForeign('journal_user_id_foreign');
});
Schema::drop('journal');
}
}
Users table :
class CreateUsersTable extends Migration
{
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');
}
}
Penulis table (pivot table to connect users table and journal table)
public function up()
{
Schema::create('penulis', function (Blueprint $table) {
// Create tabel penulis
$table->integer('id_user')->unsigned()->index();
$table->integer('id_journal')->unsigned()->index();
$table->timestamps();
$table->primary(['id_user', 'id_journal']);
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('id_journal')
->references('id')
->on('journal')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::drop('penulis');
}
}
-Edition table and Journal table has one-to-many relationship (one edition can have many journals)
-Users table and Journal table has multiple relationship, the first one is many-to-many relationship to enable a user tagging another user as one of the authors of the journal that he/she/apache helicopter upload. This relationship connected using a pivot table (penulis table). The second one is one-to-many relationship (one user has many journals) to make it possible that the user who uploaded the journal are the only one who can modify or deleting the journal (tagged users cannot).
Now back to the question, how to make this possible ? limiting the number of files that can be uploaded by users so that a user can only upload one journal in each edition. Below is my current controller to store the journal.
public function storejournal(JournalRequest $request) {
$input = $request->all();
$input['user_id'] = Auth::id();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
$id = $request->id;
$journal = Edition::findOrFail($id)->journal()->create($input);
$journal->user()->attach($request->input('penulis'));
return redirect()->route('edition', ['id' => $id]);
}
If anyone can help that'd be great, thank you and sorry for my terrible English.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire