Getting the unique year value in Laravel - laravel

I just wanted to extract unique year value from this date type column. But I always get this error "You might need to add explicit type casts".
If there are 2020-06-23, 2020-07-01, 2019-01-02, 2019-02-05 dates, my desired output is to return the unique year values. So the output should be 2020 and 2019 only.Please help. Thank you.
Here is my code:
$year= DB::table('loans')
->select('date_release', DB::raw('YEAR(date_release) as year'))
->groupBy('year')
->get();

The query could be (returns all when there are different dates from different years);
SELECT EXTRACT(YEAR FROM loans.date_release) AS year FROM loans group by year;
The query builder will be
return DB::table('loans')
->select([DB::raw('EXTRACT(YEAR FROM loans.date_release) as year')])
->groupBy('year')
->pluck('year');
it prints following for multiple years
[2018, 2019, 2020]

I think that there are only 2 ways to get what you want
first you need add another column with name year and store data only year and your query will be like this
return DB::table('loans')
->select('year')->distinct('year');
second one is before return you should some algorithm to extract year and return only distinct year it will be like this
$year = DB::table('loans')
->select([DB::raw('YEAR(date_release) as year')])
->groupBy('year')
->value('year');
$distinct_year = array();
foreach($year as $item){
$years = date('Y', strtotime($item->year))
if(!in_array($years, $distinct_year)){
array_push($distinct_year, $years);
}
}
return $distinct_year;

Related

How to get array of dates between two dates in laravel? [duplicate]

This question already has answers here:
PHP Carbon, get all dates between date range?
(11 answers)
Closed 2 years ago.
I want to get dates between two dates in an array. The scenario is the following: I want to get daily sales cash collection and there is one filter of $fromdate and $todate. So even if I don't get any sales for an specific date I have to show that in the table.
You can use CarbonPeriod for this.
I found something helpful at https://stackoverflow.com/a/50854594/13642447.
Use DatePeriod class to create the range of dates based on months or days
<?php
$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2013-10-31' );
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
$dates[] = $date->format("Y-m-d");
}
var_dump($dates);
?>
if you want the range of dates to be by days, say from 2012-08-01 to 2012-08-25 then just change the interval like this $interval = new DateInterval('P1D');.

Display sum of column between two years in laravel

I have following table i wants sum of all orders_qty in between two years & in two month. I am creating query as following which display only answer 4 and actual wants result 8(2,2,2,2)
orders_id orders_qty delivery_date
1 2,2, 2019-02-01
2 2,2, 2020-02-03
controller:getting qty addition month wise
$get_month_wise_details=DB::table('orders')
->select('orders_qty')
->where('order_status',6)
->whereBetween(DB::raw('DATE_FORMAT(deliver_date,"%Y-%m")'), ['2019-02','2020-02'])
->groupBy('orders_qty')
->get();
foreach ($get_month_wise_details as $month_wise_details)
{
$month_array=explode(',',$month_wise_details->orders_qty);
$month_addition=array_sum($month_array);
$sum_qty=$sum_qty+$month_addition;
}
controller:getting qty addition year wise
$get_year_wise_details=DB::table('orders')
->select('orders_qty')
->where('order_status',6)
->whereBetween(DB::raw('DATE_FORMAT(deliver_date,"%Y")'), ['2019','2020'])
->groupBy('orders_qty')
->get();
foreach ($get_year_wise_details as $year_wise_details)
{
$year_array=explode(',',$year_wise_details->orders_qty);
$year_addition=array_sum($year_array);
$sum_qty=$sum_qty+$year_addition;
}

Input date value and compare with existing table

I'm trying to develop a program with Laravel. I have created a table ‘tbl_holiday’ like below:
database table
I have created a form containing 02 input (date) types named ‘from_leave’ and ‘to_leave’.
I want to check these 2 inputs value(date) with the database table ‘tbl_holiday’ having the condition:
If the previous day of ‘from_leave’ match with ‘tbl_holiday.holiday_date’
Or next day of ‘to_leave’ match with ‘tbl_holiday.holiday_date’
The message “Invalid”.
Else
Message “Valid”.
Thanks in advance.
You can simply use wherebetween and check the number of data available in the collection.
$from = $request->from_date;
$to = $request->to_date;
$dates = YourModel::whereBetween('holiday_date', [$from, $to])->get();
if($dates->count() == 0){
return "valid";
}else{
return "Invalid";
}

Getting daily aggregates/sum sorted by day in Laravel

So getting a sum()/count() is really easy in Laravel...
but how would I look at the past month, and get the sum of rows every day?
EG...grouped by day that they were created at.
So I want to return a count such as 3, 2, 4, 5
Meaning 3 rows were created on todays date, 2 rows yesterday, 4 rows the day before...etc
How to do this in Laravel easily?
When I use the group by created_at it always just returns 1.
Anybody know how to do it?
Thanks
I've provided the same answer on another post. Shortening it:
$date = new DateTime('tomorrow -1 month');
// lists() does not accept raw queries,
// so you have to specify the SELECT clause
$days = Object::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`')
))
->where('created_at', '>', $date)
->group_by('date')
->order_by('date', 'DESC')
->lists('count', 'date');
// Notice lists returns an associative array with its second and
// optional param as the key, and the first param as the value
foreach ($days as $date => $count) {
print($date . ' - ' . $count);
}

finding records between date ranges returns empty in codeigniter activerecord

I have a table with a date column invoiceDate. In that there is a record with value of invoiceDate as 28-02-13
Now i use the below to fetch records between the date ranges:
$this->db->select('*');
$this->db->from('salesinvoices');
$dateone=date('Y-m-d',strtotime('26-02-13'));
$datetwo=date('Y-m-d',strtotime('12-03-13'));
$this->db->where('invoiceDate >=', $dateone);
$this->db->where('invoiceDate <=', $datetwo);
$query = $this->db->get();
$results = $query->result();
print_r($results);
I always get a empty record :(
I found the root of my issue. Strtotime sees dash as minus sign and peforms operation. I made it working using this:
date('Y-m-d',strtotime(str_replace('-', '/', '2026-02-13')));
So i replaced all dashes with / and it worked. Hope this helps someone.
I tried your code and the generated query was
SELECT * FROM "salesinvoices"
WHERE "invoiceDate" >= '2026-02-13'
AND "invoiceDate" <= '2012-03-13'
So it looks like the problem is in strtotime, which misinterprets the date.

Resources