How to Implement the Action to Determine if You Have Been Logged In with Ravel for 24 Hours

Asked 1 months ago, Updated 1 months ago, 3 views

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

2022-09-30 11:50

1 Answers

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\[email protected] {#3330
     date: 2020-12-1900:56:16.307663 Asia/Tokyo (+09:00),
   }

>>>$start=$end->subHour(24);// 24 hours ago (sample)
=>Carbon\[email protected] {#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\[email protected] {#3340
     date: 2020-12-1901:01:17.033569 Asia/Tokyo (+09:00),
   }
>>>$start=$end->subHour(24);
=>Carbon\[email protected] {#3340
     date: 2020-12-1801:01:17.033569 Asia/Tokyo (+09:00),
   }
>>>$user->last_login_at->between($start,$end)
=>true or false


2022-09-30 11:50

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.