Laravel database query for specific date - php

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();

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)

laravel eloquent with multiple where conditions issue

In my laravel based application's database, I have a table to store records of the user payments.
I'm having created_at column to store the record creation date and payment_annum column to store the subscription type(monthly or yearly)
Now I'm trying to display those records on my admin dashboard blade. But the thing is i only need to display the last month's, MONTHLY subscription records only.
So far I could retrieve data where the subscription type is monthly but struggling to filter it by the last month..
This is my current eloquent, where only checks the subscription type
$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
->get();
So how can I check the 'last month' condition as well
try changing your eloquent to this,
$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
->whereMonth('created_at', '=', Carbon::now()->subMonth()->month)
->get();
this would give give you the last month records where the payment_annum is monthly
Do you want your query to return all elements created in the last month (as of today: created_by matching 2020-03-*) or rather created during the last month (created_by >= 2020-03-23)?
For March, extend the accepted solution by
$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
->whereMonth('created_at', '=', Carbon::now()->subMonth()->month)
->whereYear('created_at', '=', Carbon::now()->subMonth()->year)
->get();
For the latter, this should do:
$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
->whereDate('created_at', '>', Carbon::now()->subMonth())
->get();

Filter SQL database query by timestamp, backend in PHP using Laravel

So I'm trying to build a query to my SQL database from my backend (that's written in php with Laravel). I have a table in my database with a column that's titled "created_at", carrying values of the 'timestamp'-type. What I want to do is, when I send a query to my database, to only pick the rows (or instances, I guess) of my table that are created 30 days or less from the current date. I've tried experimenting back and forth with the 'where' keyword, but I couldn't get it to work as I wanted it to.
When I browse the table in my database, the dates are written in the format "YYYY-MM-DD hh:mm:ss" but I'm not sure how to compare them when they're in this format, and I don't know how to convert them whilst doing a query. I've looked at the whereBetween() method in Laravel but I didn't get that to work since I didn't know how I would compare them.
At the moment my query looks like this:
$topUser = DB::table('statistics')
->join('users', 'statistics.user_id', '=', 'users.id', 'inner')
->select('fname', 'user_id', DB::raw('sum(price) as total_collection'))
->groupBy('user_id')
->orderBy('total_collection','desc')
->get();
What I want to do, in pseudocode, is something along the lines of;
->where 'created_at' <= 30 days ago
Thanks in advance!
It's basically that simple.
->whereDate('created_at', '<=', \Carbon\Carbon::now()->subDays(30));
You can take advantage of mysql's date functions by using DB::raw
->where('created_at', '<=', DB::raw("DATE_SUB(CURDATE(),INTERVAL 30 DAY)")

How to limit user requests per hour in laravel

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. ;)

Laravel: whereBetween to accept the first date

Scenario:
The startdate and enddata in the database is 2015-07-20 and 2015-07-30 respectively and the query that works properly is,
Model::whereBetween('startdate',array('2015-07-30','2015-08-10'))->get();
The query return 1 record from the table which is the expected result. Now what I expect is a slight change. The query should not retrieve the record by considering the date 2015-07-30 does not fall in between 2015-07-30. How do I achieve this?
whereBetween is inclusive. In order to exclude one of the edges you'll need to build a between query manually:
Model::where('startdate', '>', '2015-07-30')->where('startdate', '<=', '2015-08-10')->get();
Easy Solution
Model::whereBetween(DB::raw('date(startdate)'),array('2015-07-30','2015-08-10'))->get();

Categories