如何使用laravel处理一篇论文中的多个作者?



所以我在如何在一篇论文中实现多个作者以及如何将作者分开用于论文的引用方面遇到了麻烦。

现在我的输入是这样的

<div class="group">      
<input class="inputchecker2 inputInfo" id="inputID" type="text" name="Authors" required>
<span class="highlight"></span>
<span class="bar"></span>
<label class="infoLabel">Author(s)</label>
</div>

名称="Authors"像这样存储在控制器

$paper=new Papers();
$paper->Authors=$request->Authors;

一旦一篇论文中有多个作者,问题就出现了,我不知道如何实现它并使其工作,我试图为它制作另一个表,但我陷入了思考

逻辑我的迁移文件如下:表论文

public function up()
{
Schema::create('papers', function (Blueprint $table) {
$table->id('PaperID');
$table->string('PaperTitle');
$table->string('PaperType');
$table->string('Authors');
$table->string('College')->nullable();
$table->string('file');
$table->date('DatePublished')->nullable();
$table->unsignedBigInteger('UploaderUserID');
$table->foreign('UploaderUserID')->references('UserID')->on('users');
$table->timestamps();
});
}

作者表

public function up()
{
Schema::create('paper_authors', function (Blueprint $table) {
$table->id('PaperAuthorID');
$table->unsignedBigInteger('paper_ID');
$table->foreign('paper_ID')->references('PaperID')->on('papers');
$table->string('AuthorName');
$table->unsignedBigInteger('user_ID');
$table->foreign('user_ID')->references('UserID')->on('users');
$table->timestamps();
});
}

有什么方法可以简化,让我更容易理解吗?

你的方法从一开始就是错误的,

一篇论文可以属于多个作者一个作者可以有多篇论文。

在你的纸质模型中,你应该有这个
public function authors(){
return $this->belongsTo(Author::class);
}

在作者模型中,应该有这个

public function papers(){
return $this->belongsTo(Paper::class);
}

你的Paper迁移应该是这样的

public function up()
{
Schema::create('author_papers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('author_id');
$table->foreign('user_id')->references('user_id')->on('users');
$table->string('title');
$table->string('type');
$table->string('college')->nullable();
$table->string('file');
$table->date('date_published')->nullable();
$table->timestamps();
});
}

请注意,我已经将一些表列更改为Laravel约定(小写,snake_case而不是camelCase),作者的表也将这样做。我将PaperIdD更改为id,我将表名更改为author_papers。我做了很多改变,检查它们,做适合你的。重要的是将author_id添加到表

public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->id('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_ID')->references('UserID')->on('users');
$table->string('name');
$table->timestamps();
});
}

你不需要在这里加上论文编号,因为一个作者可以有多篇论文。

最后一件事,实际上是一个加分项,当创建一篇新论文时,您需要将所有作者的列表发送到HTML刀片。因此,用户将输入作者名称的部分现在将被选中,而不再输入。

Best of success

最新更新