Currently, we are trying to create a system where we cannot vote again until 24 hours after logging in as a double vote prevention function in Ravel.
I would like to create a method called logintime in the model and implement a re-voting prevention process within it.
Ravel Framework 6.18.20
source code
Article.php (Model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Article extensions Model
{
public function user(): BelongsTo
{
return$this->belongsTo('App/User');
}
public function getCountVotesAttribute(): int
{
return $this->votes->count();
}
public function logintime()
{
// Here's how to prevent re-voting.
}
}
Users Table (History of Last Login)
<?php
use Illuminate\Database\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extensions Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint$table){
$table->bigIncrements('id');
$table->string('name') ->unique();
$table->string('email') ->unique();
$table->timestamp('email_verified_at') ->nullable();
$table->string('password')->nullable();
$table->dateTime('last_login_at') ->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Thinking
True if you have logged in for 24 hours using the trinomial operator. If not, return false
public function logintime(?User$user):bool
{
return$user
? (bool) $this->votes->where('last_login_at', $user->last_login_at)// I want to process the time!
}
However, I gave up because I didn't know it would be 24 hours after processing the collection.
laravel
Curbon makes it easy to handle dates and times.Available with Ravel without additional packages
https://carbon.nesbot.com/docs/
They want to determine if it's 24 hours after logging in, so it's good to use whereBetween 24 hours before now
$phpartisan tinker
>>use Carbon\Carbon;
>>>$end=Carbon::now(); // Date and time (sample)
=>Carbon\Carbon@1608306976 {#3330
date: 2020-12-1900:56:16.307663 Asia/Tokyo (+09:00),
}
>>>$start=$end->subHour(24);// 24 hours ago (sample)
=>Carbon\Carbon@1608134176 {#3330
date: 2020-12-1700:56:16.307663 Asia/Tokyo (+09:00),
}
// True if you are logged in within 24 hours
>>User::where("id", 1) ->where Between("last_login_at", [$start, $end])->exists();
You can use last_login_at to determine if it is within 24 hours without issuing a query.This uses Carbon's between method
>>$end=Carbon::now();
=>Carbon\Carbon@1608307277 {#3340
date: 2020-12-1901:01:17.033569 Asia/Tokyo (+09:00),
}
>>>$start=$end->subHour(24);
=>Carbon\Carbon@1608220877 {#3340
date: 2020-12-1801:01:17.033569 Asia/Tokyo (+09:00),
}
>>>$user->last_login_at->between($start,$end)
=>true or false
578 Understanding How to Configure Google API Key
582 PHP ssh2_scp_send fails to send files as intended
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.