I'm trying to get all the rows I can in a Model, however it must be the last 3 possible years, as if I have a result in the year 2020 but nothing in the year 2019 then it must search in 2018, 2017, but if I had a result in 2019 then it must search only for the results in 2018.
I've already gotten the hang of whereBetween, however I don't get any results from the database using the following query:
$year = $request->all();
$fromDate = $year['yearFrom'];
$toDate = $year['yearTo'];
return Tipico::whereBetween(
DB::raw('date(created_at)'), [$fromDate, $toDate]
)
->get();
As you can see, I get the year from a post method which gives me the initial and finish year, however it doesn't validate the information I'm searching for, whats the best way to do it?
Use Carbon to create your dates:
$fromDate = Carbon::createFromDate($year['yearFrom'], 1, 1)->startOfDay();
$toDate = Carbon::createFromDate($year['yearTo'], 12, 31)->endOfDay();
Then in your query, created_at is already a datetime, so:
return Tipico::whereBetween('created_at', [$fromDate, $toDate])->get();
Remember to include Carbon in your Class at the top:
use Illuminate\Support\Carbon;
or only
use Carbon\Carbon;
To get the data from the past 3 years, one option would be to run a query to see what years have the data you need.
So, let's use the yearTo as reference to his and select the top 3 years with data:
$years = Tipico::select(\DB::raw('YEAR(created_at) as years'))
->where('created_at', '<=', $toDate)
->distinct()
->orderByRaw('YEAR(created_at) DESC')
->limit(3)
->get();
Now in $years you will have the 3 years. Now you can run the query with this years as parameter:
return Tipico::whereIn(\DB::raw('YEAR(created_at)'), $years)->get();
*Not sure if you can pass $years directly or need to transform to array. Take a look.
Related
I'm trying to get the employees that have worked for more then 6 months and how many days have passed since that day.
I've fetched the employees with six months experience using the following query:
$empsWithSixMonthsExp = Employees::whereDate('joining_date', '<=', Carbon::now()->subMonths(6))->where('status', 1)->get();
But now I want to get the date when their 6 month was fulfilled and how many days have passed since completing their 6 month mark.
Thanks!
Here is the example for single record
$joiningDate = '05/06/2021';
$AfterSixMonthDate = \Carbon\Carbon::createFromFormat('d/m/Y',
$joiningDate)->addMonths(6);
$todaysDate = \Carbon\Carbon::now();
$daysDifferent = $todaysDate->diff( $AfterSixMonthDate)->format('%a Days');
you can dump this code
dd( $daysDifferent,$AfterSixMonthDate,\Carbon\Carbon::now());
I have a date list in the database and from the list, I get the month and despite lay in the dropdown. Right now it works but I don't know how to order the date by ascending before I change the format. Below is the date list:
date
2021-08-02
2021-09-30
2021-08-01
2021-09-06
2021-11-05
Right now in the dropdown display
August
November
September
It should be
August
September
November
Here's my eloquent to display the dropdown
$date = DateList::where('id',$id->id)->select(DB::raw("(DATE_FORMAT(date, '%M')) as newDate"))->groupBy('newDate')->get();
I have a search around but still could not find the answer on how to get the correct way to order the date before changing it to the format. Hope anyone can help.
Why don't you use laravel's awesome collection api?
DateList::where('id',$id->id)
->orderBy('date', 'ASC')
// uncomment the below line if you're concerned with performance
// ->select('date')
->pluck('date')
->map(function($date) {
return $date->format("F");
})
->unique();
Hey GarfieldJuniorI couldn't think any way to implement the above but the way I would suggest is to fetch month's number instead of name and sort it then map the array/Collection to Months name,
for mapping thing either you can user index array of like
$monthNames=[
1=>"JANUARY",
2=>"FABRUARY
]
OR
you can use by creating a dummy date by months Number using date() method of php, It takes format type of string as first argument and second $timestamp
$format ='F' ,
and
$timestamp=mktime(0,0,0,$monthNumber)
ref mktime
so in short your if you get sorted months number Array like this (I'm using date method not indexed Array)
$monthsArray=[1,2,3];
$monthNames=array_map(
function($monthNumber){
return date("F", mktime(0, 0, 0, $monthNumber));
}
,$monthsArray);
your output will look like this
[
"January",
"February",
"March",
]
You can do order by from your query like this
$date = DateList::where('id',$id->id)
->select(DB::raw("(DATE_FORMAT(date, '%M')) as newDate"))
->groupBy('newDate')
->orderBy('newDate', 'ASC')
->get();
How can we add days in the date that we're querying using laravel eloquent?
Something like this:
Post::where('created_at + 7 days', now()->toDateTimeString())
->where('created_at', '<', now()->toDateTimeString())
->get();
My logic is like this one
today >= created_at && today <= created_at + 4
Is there some correct syntax to achieve this one?
One of the ways to compare from date time is using the calculated Carbon like below:
Post::where('created_at', '>=', now()->addDays(4)->toDateTimeString()
->get();
It will display only posts which have created_at greater than 4 days from today.
For date range filter, you can use between scope like:
$startDate = now()->subDays(4)->toDateString();
$endDate = now()->toDateString();
$filteredPosts = Post::whereBetween('created_at', [$startDate, $endDate])->get();
For more information about filter, you could visit Laravel Query Builder
You can use whereDate method:
Post::whereDate('created_at', 'like', now()->subDays(4))->get();
or
Post::whereDate('created_at', '>=', now()->subDays(4))->get();
I'm trying to retrieve data from the past 30 to 60 days. Which means I'm leaving out days 0 to 30 and everything past day 60.
My current progress is as such:
$previousMonth = DB::table('table')
->select('table.*')
->whereBetween('date', [Carbon::now()->subDays(30), Carbon::now()->subDays(60)])
->get();
return $previousMonth;
"date" has the datatype of DATETIME in mysql database.
Change the order, because Carbon::now()->subDays(30) is greater than Carbon::now()->subDays(60), and you can use startOfDay() to get start of day:
->whereBetween('date', [Carbon::now()->subDays(60)->startOfDay(), Carbon::now()->subDays(30)->startOfDay()])
Try this startOfDay()
use Carbon\Carbon;
$previousMonth = DB::table('table')
->select('table.*')->whereBetween('date', [Carbon::now()->subDays(60)->startOfDay(), Carbon::now()->subDays(30)->startOfDay()])->get();
return $previousMonth;
Refer This Link
http://carbon.nesbot.com/docs/
I am working on a booking system and need to select only the records where a certain field is for today. Not today and in the future, but only today.
The query currently is:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', ?????);
$advancedBookings->get();
The requested_booking_time is the field that I wish to be checked, the date stored is in this format:
2017-08-23 08:00:00
So I want to only select rows that are on the same day as the current day.
As i understand, you want that records which is created today; then just get the today's date.
$today = date("Y-m-d");
now your query is like this.
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', $today)->get();
You can use whereDate and format your dates with Carbon:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
$advancedBookings->get();
Or format the your date before the query with Carbon.
You should try this:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=', date('Y-m-d'))
->get();