Laravel get rows with specific day and month - php

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

Related

Get records from two date columns in db in laravel via eloquent

i have a leaves table called user_leaves in which leaves taken by users are stored, table has this structure.
id, leave_type_id, from_date, to_date, status, created_at, updated_at.
leave_type_id is basically a foreign key of types of leaves my system has.
I want to fetch all leaves taken by a user in a range of two dates like start_date and end_date (for example all leaves from 2020-08-01 to 2020-08-31 .
I want records in numbers like,
{
"user_id" : 1,
"leaves" :
{
"medical_leave" : 2,
"earned_leave" : 5,
"sick_leave" : 3
}
}
I used the code
$UserLeaves = UserLeave::whereDate('date_from', '>=', $start_date)
->whereDate('date_from', '<=', $end_date)
->get();
The problem is that if i choose from_date to query my result ,and then loop through $UserLeaves to get all days between from_date and to_date for each leave_type.
Their are cases when to_date may exceed my end_date and thus giving wrong data if i calculate all days between from_date and to_date.
You would do something like this
$baseQuery = $baseQuery->where(function ($query) use ($date_created_from, $date_created_to) {
if (isset($date_created_from) && $date_created_from != '') {
$dateFrom = Carbon::parse($date_created_from)->format('Y-m-d');
$query->whereDate('date_column', '>=', $dateFrom);
}
if (isset($date_created_to) && $date_created_to != '') {
$dateEnd = Carbon::parse($date_created_to)->format('Y-m-d');
$query->whereDate('date_column', '<=', $dateEnd);
}
});
Where
$baseQuery is the query you write to perform all your selections, aggregation etc.
The above code accounts for null values and additionally you could perform additional checks before appending the query to your base query like "date from is not greater than 1 week before today".
you can use whereBetween clause and in wherebetween you can pass $from_date and $to_date as below:
$from = date('2018-01-01');
$to = date('2018-05-02');
youModal::whereBetween('column_from', [$from, $to])->get();

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

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

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

Categories