Count the users created within last week in Laravel - php

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

Related

How to use the Larave DB Facade to get the first record with a date close to a particular date

I am trying to get the first transaction that was recorded close to a particular date using the Laravel 8 DB Facade.
I have tried to use it in the following manner
$lastest_transaction = DB::table($targetTable)
->where('date', '<=', date('F'))
->where('id', '=', $client->id)
->where('branch', '=', session('branch'))
->get();
but it's not giving me the correct transaction.
In order to get the record closest to the date, you can sort by the date in descending order, which would make the first record the "largest" date that's less than or equal to what you are searching for:
$lastest_transaction = DB::table($targetTable)
->where('date', '<=', date('F'))
->where('id', '=', $client->id)
->where('branch', '=', session('branch'))
->orderBy('date', 'DESC') // Order by the date in descending order
->first(); // Get the first record

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

How to Get Last Seven Days in Laravel?

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

Laravel - group data by created_at field and apply pagination

I need to get previous 10 days activity of a user. In my case i am just getting 10 activity records instead of previous 10 days activity.
*My Code:
$activity = $this->where('user_id', '=', $userId)->paginate(10);
$activities = collect($activity->items())->groupBy(function($item) {
return \Carbon\Carbon::parse($item->created_at)->format('d-M-Y');
});
$activity->setCollection($activities);
dd($activity->toArray());
Output:
You are using paginate which restricts the number of results fetched from the DB. If you want to get previous 10 days activity of a user then you can write your query as:
$activity = $this->where('user_id', '=', $userId)
->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))
->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