Check if user has been authenticated today - laravel

I am new to laravel and i want to check if the user has been authenticated today so he can collect the points.
The day must start at 00:00:00 and end at 00:00:00
What can be the logic behind that ?
Thanks for looking!

you need to add last_login_date column in your users table and in any time the user has authenticated you shold update this column and you can check if the user authenticated today user compare beetween today date and last_login_date
you can use carbon api
$startDate = \Carbon\Carbon::createFromFormat('Y-m-d','2019-10-01');
$endDate = \Carbon\Carbon::createFromFormat('Y-m-d','2019-10-30');
$check = \Carbon\Carbon::now()->between($startDate,$endDate);

Related

Laravel, how to delete all records but the first one of that day

So I have a table to log profile followers every 10 minutes to keep a record of the increase/decrease.
However after the current day I only need to keep the last record of that day. Is there a simple way in Laravel to delete all records a part from the last one recorded every day.
I've tried searching and searching but comes up with nothing and feel like I'm going to create something overly complicated to accomplish this.
You'd need a query like this.
DELETE
FROM logs
WHERE id IN (
SELECT id
FROM logs
WHERE created_at BETWEEN 2022-02-28 00:00:00 AND 2022-02-28 23:59:59
ORDER BY created_at DESC
OFFSET 1
)
Assuming you have a date variable, you could make the query like this using the whereDate method:
$date = '2022-02-28'; // Y-m-d format. This is important.
DB::table('logs')
->whereIn('id', function ($sub) use ($date) {
$sub->select('id')
->from('logs')
->whereDate('created_at', $date)
->orderByDesc('created_at')
->offset(1);
})
->delete();

Laravel 8 - count clicks but user can't spam the function

In my website I track clicks on a specific link and save the count on database with this function:
public function redirect($url)
{
$data = Class::where('url', $url)->first('clicks');
$cnt = $data->clicks;
$cnt++;
$sql = Class::where('url', $url)->update([
'clicks' => $cnt,
]);
if (!$sql) {
abort(500);
}
return redirect('https://website.com/'.$url);
}
the problem is if someone spam the link , the count goes up. I want stop count if user clicked on link 5mins ago.
he will redirected to the link but count doesn't go up.
I'm new so it's so good if you explain it with details. Thanks
I would create a new table, lets call it link_clicks. You will need 3 columns, one to identify the person, one to get the time and one to identify the link (I dont exactly know how you store the links you want to observe).
So more or less you will have the following table:
link_clicks
user_id
link_id
created_at
If the users are always logged in, I would store the user_id, if they are not logged in, I would store the IP-address. So instead of user_id make a column called ip or ip-address.
Afterwards you can easily get the last click and its time.
--Example
Your new table contains following entry:
user_id = 1, link_id = 1 and created_at = 2021-04-21 19:00:00
Now in your controller you get the current date_time date("Y-m-d H:i:s") and the user id like that: auth()->id(). You can also define your time treshold in minutes e.g max_dif = 5.
Afterwards you can query the table for all clicks for the user. You can either make the time comparision in your where() statement or you can make some comaprison in your php code to check if the time treshhold is reached.

How to check if voucher is expired after it purchased one year.?

I am building a voucher generate system in laravel. This voucher validity period is one year. Now i want do is when user trying to redeem a voucher then user cannot do it because of it is expired. Any one can help me to do this. Because I don't have any idea about this.
This is what I am tried
$voucher = Voucher::where('code', $request->code)->first();
$today = date("Y-m-d H:i:s");
$startdate = $voucher['created_at'];
$offset = strtotime("+365 day");
$enddate = date($startdate, $offset);
$today_date = new \DateTime($today);
$expiry_date = new \DateTime($enddate);
if ($expiry_date < $today_date) {
return response()->json(['error' =>"Voucher is expired"]);
}
Assuming you're storing the vouchers in the database, you can add a valid_until date column and verify that it's in the past before redeeming the voucher.
Basing the expiry date off the created_at field could be problematic in the future if the expiry rules change. For example consider what would happen if after a while you decide that vouchers should actually expire after 6 months. If I had purchased a voucher which was expected to last a year then it wouldn't be fair that mine expires early.
A better approach as mentioned in a previous answer would be to store the expiry date in a separate column in the table. This would make the code that checks for expiry much simpler and also would allow you to have different expiry times if required, e.g. a premium voucher could last for 2 years instead of just one.

Laravel carbon and subDays

I am trying to get list of users with created_at date more than 30 days but it get users with even today date.
Logic
Get users which registration dates +30 days.
If their verification date is null delete them.
Issue
even if I register today and my verification date is null my account will delete.
Code
$date = Carbon::today()->subDays(30);
$deleteUsers = User::where('email_verified_at', null)->where('created_at', '>', $date)->delete();
$date=Carbon::today()->subDays(30);
$deleteUsers = User::where('email_verified_at', null)->where('created_at', '<', $date)->delete();
We compare this date code with the previous one if it was smaller
code:
$date=Carbon::now()->subDays(30)->format('Y-m-d');
$t=Trash_form::where('updated_at','<=',$date)->value('id');
return Trash_form::where('id',$t)->delete();

Check today is between start date and expired date using Carbon?

I have to check whether coupon is active, or no by checking that today is between start date and expired date.
My start date and expired date format is 01/12/2017
Let assume, today is 21/06/2017
Coupon start date is 19/06/2017 and expired date is 23/06/2017
So it should result in Coupon is Active, wheter today is less than coupon start and expired date it should result in Coupon is Expired
So far I didn't find any tutorial which comparing today with start and expired date using Carbon.
Almost all tutorial like this
$now = Carbon::now();
$end_date = Carbon::parse($request->input('end_date'));
$lengthOfAd = $end_date->diffInDays($now);
So How I can check if today is between start date and expired date, and today is less than start date and expired date using Carbon in Laravel?
Thanks in advance.
It's a very simple task using Carbon, all you can do is call the between method of carbon :
$now = Carbon::now();
$start_date = Carbon::parse($request->input('start_date'));
$end_date = Carbon::parse($request->input('end_date'));
if($now->between($start_date,$end_date)){
echo 'Coupon is Active';
} else {
echo 'Coupon is Expired';
}
You can add a third parameter to the between method to use or equal like discribed in the doc
To determine if the current instance is between two other instances you can use the aptly named between() method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries.

Resources