laravel 用户只在一个表单中投票一次
laravel user only vote once in a form
我对我想制作的用户投票系统有疑问。
是否可以在用户模型中建立某种角色模型,如果用户投票(这是他们填写的表格),他们将无法查看该页面或无法再次提交表格,因为他们已经投了一次票。
但我不确定这是否可能,你知道是否有办法使这成为可能?
更新
用户模型:
protected $table = 'users';
protected $fillable = ['email', 'password', 'voted'];
protected $hidden = ['password', 'remember_token'];
选项型号:
protected $table = 'options';
protected $fillable = ['id, points'];
用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->boolean('voted')->default(0);
$table->rememberToken();
$table->timestamps();
});
}
选项迁移
public function up()
{
Schema::create('options', function(Blueprint $table)
{
$table->increments('id');
$table->string('option');
$table->tinyInteger('points');
$table->timestamps();
});
}
可能很高兴知道,在我的 RoundOneController@update 中我有 2 个 If else 语句。
(如果 select 框 1 是来自数据库的 ID,则更新,否则创建新的。与 select 框 2 相同)
但是,如果有可能在这结束时用户 table 将被更新并且投票列将更改为 1,那么用户将无法再投票。
虽然没有看到您的代码是如何设置的,但实际上有几种方法可以实现这一点。
一种方法是在您的 User
模型中有一个 voted
列。如果您在迁移中将其设置为 boolean
,默认值为 0,
Schema::table('users', function ($table) {
$table->boolean('voted')->default(0);
});
然后您可以在用户投票后将其设置为“1”。然后为检查此值是否存在的投票页面设置Middleware
。
中间件文件:
public function handle($request, Closure $next)
{
if (Auth::user()->voted) {
return redirect('home');
}
return $next($request);
}
确保在 kernal.php
中注册中间件
protected $routeMiddleware = [
......
'voted' => \App\Http\Middleware\RedirectIfVoted::class,
];
并将其应用于您的路线:
Route::get('user/vote', ['middleware' => ['voted'], function () {
//
}]);
我对我想制作的用户投票系统有疑问。
是否可以在用户模型中建立某种角色模型,如果用户投票(这是他们填写的表格),他们将无法查看该页面或无法再次提交表格,因为他们已经投了一次票。
但我不确定这是否可能,你知道是否有办法使这成为可能?
更新
用户模型:
protected $table = 'users';
protected $fillable = ['email', 'password', 'voted'];
protected $hidden = ['password', 'remember_token'];
选项型号:
protected $table = 'options';
protected $fillable = ['id, points'];
用户迁移
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('password');
$table->boolean('voted')->default(0);
$table->rememberToken();
$table->timestamps();
});
}
选项迁移
public function up()
{
Schema::create('options', function(Blueprint $table)
{
$table->increments('id');
$table->string('option');
$table->tinyInteger('points');
$table->timestamps();
});
}
可能很高兴知道,在我的 RoundOneController@update 中我有 2 个 If else 语句。 (如果 select 框 1 是来自数据库的 ID,则更新,否则创建新的。与 select 框 2 相同) 但是,如果有可能在这结束时用户 table 将被更新并且投票列将更改为 1,那么用户将无法再投票。
虽然没有看到您的代码是如何设置的,但实际上有几种方法可以实现这一点。
一种方法是在您的 User
模型中有一个 voted
列。如果您在迁移中将其设置为 boolean
,默认值为 0,
Schema::table('users', function ($table) {
$table->boolean('voted')->default(0);
});
然后您可以在用户投票后将其设置为“1”。然后为检查此值是否存在的投票页面设置Middleware
。
中间件文件:
public function handle($request, Closure $next)
{
if (Auth::user()->voted) {
return redirect('home');
}
return $next($request);
}
确保在 kernal.php
protected $routeMiddleware = [
......
'voted' => \App\Http\Middleware\RedirectIfVoted::class,
];
并将其应用于您的路线:
Route::get('user/vote', ['middleware' => ['voted'], function () {
//
}]);