How to Get Last Seven Days in Laravel? - php

I am trying last seven days record using below query, but it does not work.
<?php
DB::table('data_table')
->select(DB::raw('SUM(fee) as counter, left(DATE(created_at),10) as date'))
->whereIn('user_id', $descendants)
->whereRaw('DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
->groupBy(DB::raw('left(DATE(created_at),10)'))
->get();
How to make it correct?

->where('created_at', '>=', Carbon\Carbon::now()->subDays(7)->startOfDay())->get()

Related

Laravel query by date and time

Query by date and time.
I have table with field and sample data
Schedule
--date_start = 2019-05-01
--date_end = 2019-06-30
--time_start = 08:00:00
--time_end = 10:00:00
My question is can I query it by doing select date by (current_date) if current_date is in range or in between the date_start and date end. the same also in time if time is in range
thanks in advance.
You can do it with something like this
$current = Schedule::whereDate('start_date', '<=', today())
->whereDate('end_date', '>=', today())
->whereTime('start_time', '<=', now())
->whereTime('end_time', '>=', now())
->get();
$current will be the DB records that are for now.
More information at the Docs

Count the users created within last week in Laravel

How to use carbon and count the users created in the last 7 days. On blade, I have a table that shows the total of users created per day that has this syntax (example is for today)
$getusertoday = MemberProfile::where('encoded_by', '=', $profile->name)
->whereDate('created_at', '=', $today )->count();
But in the last column, which is the Weekly Total, I must total everything.
$getusertoday = MemberProfile::where('encoded_by', '=', $profile->name)
->whereDate('created_at', '>=', date('Y-m-d H:i:s',strtotime('-7 days')) )->count();

Having trouble getting records using Laravel

I have the following MySQL query:
SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY;
When I run that in MySQL it gives me the number of records for the past day.
I'm trying to do this in Laravel, because it's included in the WHMCS software I'm using, and can't really understand how to make it work.
Sometimes, when you are stuck in Laravel, just paste your SQL into a raw query like
$result = DB::select("SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY");
Using a query builder
$result = DB::table('tblname')
->whereDate('date', '>=', Carbon::today())
->whereDate('date', '<=', Carbon::tomorrow())
->count();
You can use carbon like
$query = DB::table('your table')->whereDate('date', '>=', Carbon::today())->whereDate('date', '<=', Carbon::tomorrow())->count();

Get record from database inserted in last two hours using Laravel 5.1

I want to get all the record from the database inserted in last two hours in Laravel 5.1. I am able to get the record for last 1 hour by using the following code:
$all_bridal_requests_check2 = \DB::table('bridal_requests')
->where(function($query)
{
$query->where('publisher', '=', 'bq-quotes.sb.com')
->orWhere('publisher', '=', 'bq-wd.com-bsf');
})
->where('created_on', '>=', \Carbon\Carbon::now()->subHour())
->orderBy('created_on', 'desc')
->get();
use MySQL NOW() or INTERVAL
WHERE `created_on` > NOW() - INTERVAL 2 HOUR
in Laravel
->where('created_on', '>', NOW() - INTERVAL 2 HOUR)
Instead of "subHour", use "subHours(2)"
->where('created_on', '>=', \Carbon\Carbon::now()->subHours(2))
Try this:
$all_bridal_requests_check2 = \DB::table('bridal_requests')
->where(function($query)
{
$query->where('publisher', '=', 'bq-quotes.sb.com')
->orWhere('publisher', '=', 'bq-wd.com-bsf');
})
->where(\DB::raw('date >= DATE_SUB(NOW(), INTERVAL 2 HOUR)')) ->count()
->orderBy('created_on', 'desc')
->get();

Laravel get rows with specific day and month

I have the following query in laravel:
$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->where('tbl_users.birth_date', '>=', date("m-d")) //my error
->where('tbl_events.year', '>=', date("Y"))
->get();
I have a users table (tbl_users), with the date of the birthdays of each user. And an events table (tbl_events), which records the anniversary of each user. Thus creating a table that will get the next birthdays.
My birth_date is type "yyyy-mm-dd". The table events is the year. I want to get the next event on the current day and year.
ie I think the anniversary date must be greater than the current month and day, and the event with the top year to the current year
Format the birth_date to mm-dd first before comparing it to date('m-d'), like so:
$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
->where('tbl_events.year', '>=', date("Y"))
->get();
Update
If your tbl_events doesn't have a full date attribute, you could do this in 2 queries:
First, get the remaining events of this year:
$events_this_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
->where('tbl_events.year', '=', date("Y")) // Get remaining events this year
->get();
Second, get the events for next years:
$events_following_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->where('tbl_events.year', '>', date("Y")) // Get events for upcoming years
->get();
Then merge them
$eventos = $events_this_year->merge($events_following_year);

Categories