如何在Laravel中实现单篇论文多作者管理及引用分离?
Laravel 单篇论文多作者功能简化实现方案
一、数据库结构调整
移除
papers表的冗余字段
先删掉papers表中原来的Authors字符串字段,因为后续用关联表存储多作者信息:
生成迁移文件:php artisan make:migration remove_authors_from_papers迁移代码:
public function up() { Schema::table('papers', function (Blueprint $table) { $table->dropColumn('Authors'); }); } public function down() { Schema::table('papers', function (Blueprint $table) { $table->string('Authors'); }); }优化关联表(可选)
如果允许添加非系统注册的作者,把paper_authors表的user_ID设为可空:
生成迁移文件:php artisan make:migration update_paper_authors_table迁移代码:
public function up() { Schema::table('paper_authors', function (Blueprint $table) { $table->unsignedBigInteger('user_ID')->nullable()->change(); }); } public function down() { Schema::table('paper_authors', function (Blueprint $table) { $table->unsignedBigInteger('user_ID')->nullable(false)->change(); }); }(若作者必须是系统用户,此步骤可跳过)
二、定义模型关联
Paper模型(app/Models/Paper.php)
建立论文到多作者的一对多关系:namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class Paper extends Model { protected $primaryKey = 'PaperID'; protected $fillable = ['PaperTitle', 'PaperType', 'College', 'file', 'DatePublished', 'UploaderUserID']; public function authors(): HasMany { return $this->hasMany(PaperAuthor::class, 'paper_ID', 'PaperID'); } }PaperAuthor模型(app/Models/PaperAuthor.php)
建立作者到单篇论文的从属关系:namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class PaperAuthor extends Model { protected $primaryKey = 'PaperAuthorID'; protected $fillable = ['paper_ID', 'AuthorName', 'user_ID']; public function paper(): BelongsTo { return $this->belongsTo(Paper::class, 'paper_ID', 'PaperID'); } // 关联用户(可选,若作者是系统用户则保留) public function user(): BelongsTo { return $this->belongsTo(User::class, 'user_ID', 'UserID'); } }
三、表单提交与后端处理
1. 前端表单(简化版)
用逗号分隔多作者,快速实现输入:
<form method="POST" action="{{ route('papers.store') }}" enctype="multipart/form-data"> @csrf <!-- 其他论文字段:标题、类型、文件等 --> <div> <label>作者(多个用逗号分隔)</label> <input type="text" name="authors" required> </div> <button type="submit">提交论文</button> </form>
(进阶可加JS动态添加输入框,进一步优化体验)
2. 控制器逻辑(PapersController.php)
存储论文的同时,拆分作者信息存入关联表:
namespace App\Http\Controllers; use App\Models\Paper; use App\Models\PaperAuthor; use Illuminate\Http\Request; class PapersController extends Controller { public function store(Request $request) { // 字段验证 $validated = $request->validate([ 'PaperTitle' => 'required|string', 'PaperType' => 'required|string', 'authors' => 'required|string', 'file' => 'required|file', // 其他字段验证规则按需添加 ]); // 存储论文主记录 $paper = Paper::create([ 'PaperTitle' => $validated['PaperTitle'], 'PaperType' => $validated['PaperType'], 'College' => $request->College, 'file' => $request->file('file')->store('papers'), 'DatePublished' => $request->DatePublished, 'UploaderUserID' => auth()->id(), ]); // 拆分作者并写入关联表 $authorNames = array_map('trim', explode(',', $validated['authors'])); foreach ($authorNames as $name) { // 若需关联系统用户,可在此加逻辑:比如根据用户名查找user_ID $userId = null; PaperAuthor::create([ 'paper_ID' => $paper->PaperID, 'AuthorName' => $name, 'user_ID' => $userId, ]); } return redirect()->route('papers.index')->with('success', '论文提交成功'); } }
四、论文引用时获取作者列表
在论文详情页直接从关联关系中取作者,格式化展示:
<div class="paper-authors"> <h4>作者:</h4> @foreach($paper->authors as $author) <span>{{ $author->AuthorName }}</span>@if(!$loop->last), @endif @endforeach </div> <!-- 示例:APA格式引用 --> <div class="citation"> <p> {{ $paper->authors->pluck('AuthorName')->implode(', ') }}. ({{ $paper->DatePublished?->format('Y') }}). {{ $paper->PaperTitle }}. {{ $paper->College ?? '' }}. </p> </div>
额外简化提示
如果作者都是系统内注册用户,可以直接用多选框让用户选择用户,后端直接存储user_ID,paper_authors表甚至可以去掉AuthorName字段,直接从用户表读取姓名,结构更简洁。
内容的提问来源于stack exchange,提问作者Dynow
相关产品推荐
相关产品推荐

