How to limit user requests per hour in laravel - php

I have a site that has few request options (for example add friend). I want to limit of requests made by use per certain time, for example so he can't send more than 5 friend requests within hour.
QUESTION How Can I achieve this?
I have written following query
$result = DB::table('bubble_users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '=', "pending")
->where('user_id','=', $user_id)
->where(DB::raw('DATE_ADD(created_at, INTERVAL 1 HOUR) > NOW()'))
->groupBy('status')
->get();
But I'm not getting wrong response
Anybody help me!!

Just get last row of this user on table and get created_at column?
Then make another query to get count requests by this user in the our on the last 'created_at`query value.
So you can know if this our has 1, 2, 3, 4 or five requests.
Is very simple. ;)

Related

Laravel query builder count rows by day SQL db::raw

I've got a Laravel 8 application whereby I need to count the number of total applications for each day and output a count of the data in the most efficient way, for instance, 500 applications on 1st, 1,000 applications on 2nd and so fourth, depending on how many applications there are each day.
I'm using DB::raw() to count my rows, however, Laravel's timestamps by default includes the hours, minutes and seconds as part of the date, and so this isn't going to work for me, I need to format the date to exclude this and just contain the day.
My query doesn't seem to be returning anything, and no error either, so I think there's something wrong with my first DB::raw(), it doesn't seem to be formatted the created_at column at all, what am I missing?
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT("created_at, %Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupBy('created_at')
->get();
You can use groupByRaw()
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupByRaw('DATE_FORMAT(created_at, "%Y-%m-%d")')
->get();
Also, you can use selectRaw() instead of select(DB::raw)

How to retrieve records from database table in Laravel and prevent those which are expired

So I have a table in MySQL which looks like this
these are basically ads that are posted by users. Duration column in this table is about for how many days the User wants to show their ad. that means if user choose to post ad for 3 days i should not just show after 3 days that means it will expire after three days. Now i Want to retrieve only those records which are not expired.
I have searched a lot but could not find a way to do this. Kindly give me a solution or refer to a link where i can possibly find the solution.
Right now I am using this script to get the records with simple pagination
$items=Post::with('items','users')->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
You should do an Where Clause with your paginate.
as stated here: https://stackoverflow.com/a/50903963/3808783
like:
$items=Post::with('items','users')
->where('created_at', '<=', Carbon::now()->addDays(3))
->paginate(12);
Use Carbon to do so.
Simply add duration to created_at with Carbon addDay($duration) method.
Then return your posts where the result is >= to today().
So I changed my table to this
And using this script
$items=Post::with('items','users')->where('duration','>=',\Carbon\Carbon::now() )->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));
It is working perfectly. Thanks to every one.

How do I check if column is null within query with eloquent?

I am trying to delete all entries which have not been viewed in the past 30 days and are were created more than 30 days ago.
The column last_viewed_at's default value is null, so in case the entry was created_at 5 days ago and was not viewed, the row should get not be deleted.
I currently use
$medias = Media::where('last_viewed_at', '<=', $delete_after_x_days)->get();
This code snippet does not include the scenarios of where a media item was created during the last 30 days and has not been viewed, so should not be deleted just yet.
Somehow, I need to check created_at if last_viewed_at is null to make sure the media is older than 30 days.
Any help would be greatly appreciated!
i have no idea in your model how you use but your where condition would look like this,
last_viewed_at is not null and last_viewed_at<= $delete_after_x_days
Finally, I came up with a solution thanks to #knowledge....'s hint.
$medias = Media::where('created_at', '<=', $delete_after_x_days)
->where(function ($medias) use ($delete_after_x_days) {
$medias->where('last_viewed_at', '<=', $delete_after_x_days)
->orWhereNull('last_viewed_at');
})
->get();
This makes sure the entry is older than x days and that last_viewed_at is also older than 30 days or is null.

(LARAVEL) Two ORDER BY and a LIMIT

This is some sorting:
Get 20 posts sorted by "views".
AFTER IT FINISH, then go to the next step.
sort it by "created_at".
How to do it?
Heres my current code (which work, but I dont want to use SORTBY) for some reason sortby is not working for other project. if possible i want it to be as one eloquent query:
$this->data['today_post'] = Posts::orderBy('views', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
$this->data['today_post'] = $this->data['today_post']->sortByDesc('created_at');
This code is not working, because LIMIT is usually applied as the last operation, so the result will first be sorted by "views" and "created_at" and then limited to 20. I dont want that. I want to sort and limit then after all is complete. I want to sort again the last time.
$this->data['today_post'] = Posts::orderBy('views', 'desc')->orderBy('created_at', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
Thank you so much
You'll still need to use sortBy() or sortByDesc() if you want to use Eloquent. I've just tested this solution and it works perfectly:
Posts::orderBy('views', 'desc')
->where('created_at', '>=', Carbon::now()->subDay())
->take(20)
->get()
->sortByDesc('created_at');

Laravel database query for specific date

Please help guys. I have my database:
Moreless...
I want to build a query that will count each column with a specific date for a specific page. For example Page 1 have visits 25th Dec and 26th Dec, but I want count every visit on 26th.
Somebody could help me even with pure MySQL?
This query will give you how many visitors does a specific page has on specific date. I am not sure if this is what you aim for but that's what I understood from the question.
SELECT DATE(created_at) AS date_visited, page_id, COUNT(1) AS visits
FROM your_table_name
GROUP BY DATE(created_at), page_id
As explanation - it is a basic GROUP BY usage, you can refer to the MySQL documentation http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html if you are not familiar with these. It has nothing to do with Laravel tho.
You can do it using whereDate and groupBy as:
Model::select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();
Or you can also use query builder as:
DB::table('table_name')
->select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();

Categories