我在我的应用中有这种情况,我必须限制用户可以写的帖子。例如,我有一个名为Edition的表来存储版本数据,而Article表用于存储文章数据。版本表和文章表具有一对多的关系(一个版本有很多文章)。现在,我有一个Users表来存储用户数据,这个表与Article表有一对多的关系(一个用户有很多文章)。切入案例,我想制定一个规则,用户在每个版本中只能有一个文章。我们只是说有一个名为“日本食品”的版本记录,在这个版本记录中,用户可以发布他们的文章,但只有一篇文章。已经在此“日本食品”版本中发布文章的用户只能在另一个版本记录中发布新文章,但尚未发布任何文章。现在我的问题是,如何在Laravel中制定这样的规则?尤其是Laravel 5.3。我创建了我的迁移和模型,下面是代码(如果有必要帮助您回答我的问题)。
版表:
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');
}
文章表:
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');
}
用户表:
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');
}
我的控制器:
public function storeArticle(ArticleRequest $request) {
$input = $request->all();
$input['user_id'] = Auth::id();
$idi = $request->id;
$id_edition = Edition::findOrfail($idi);
$user = Auth::user();
if ($this->userPolicy->userHasArticle($user, $id_edition)) {
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert article datas
$article = Edition::findOrFail($idi)->article()->create($input);
$article->user()->attach($request->input('penulis'));
return redirect()->route('edition', ['id' => $idi]);
} else {
return 'Error! You already posted your article in this edition, please pick another edition instead';
}
}
谢谢你的帮助! :)
答案 0 :(得分:2)
可行的解决方案是你只需要制作UserPolicy及其中的方法来检查用户是否已经创建了该文章。喜欢这个
UserPolicy将采用以下方法:
public function userHasArticle(User $user, $edition_id){
//Made Article Model
$article = Article::query()->where('user_id', $user->id)->where('id_edition', $edition_id)->get();
if(is_null($article)){
return true;
}else{
return false;
}
}
虽然控制器逻辑应该是这样的:
protected $usersPolicy;
public function __construct(UsersPolicy $usersPolicy)
{
$this->usersPolicy = $usersPolicy;
}
public function post(Request $request){
//First check like this
//Get the id_edition and before processing anything first check this condition
if($this->usersPolicy->userHasArticle(auth()->user(), $id_edition)){
}else{
//Return Error!
}
}
确保UserPolicy具有use HandlesAuthorization
特征
希望!你得到了我的观点。如有任何疑问,请在下:)
答案 1 :(得分:0)
它现在真的有效,所以我在我的控制器中做了类似的事情而没有使用政策(如果有人需要的话,请在这里写下来)
public function storearticle(ArticleRequest $request) {
$input = $request->all();
$input['user_id'] = Auth::id();
$idi = $request->id;
$articlerules = Article::where('id_edition', $idi)
->where('user_id', $request->user()->id)
->exists();
if ($articlerules == true) {
return 'You already have an article in this edition, please pick another edition instead.';
} else {
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert article datas
$article = Edition::findOrFail($idi)->article()->create($input);
$article->user()->attach($request->input('authors'));
return redirect()->route('edition', ['id' => $idi]);
}
}