Counting years in y/m/d format in laravel - laravel

I have these as a sample of records
2016/01/13, 2016/01/15, 2016/01/25, 2015/01/14, 2015/01/17, 2015/01/28 and so on.
Any suggestions on how to only count (yyyy) where the month of January appears so that I get 2.(2016,2015)
$stocks = DB::table('inventories')
->select('inventories.*',DB::raw('count(*) as count,date_sold'))
->whereMonth('complete_sold','=', Carbon::today()->month)
->groupBy('inventories.drug_id')
->get();

Option 1:
If you only need the count number and not the records from the database do this:
$stocks = DB::table('inventories')
->whereMonth('complete_sold','=', Carbon::today()->month)
->groupBy('inventories.drug_id')
->count();
This way $stock will equal to number of records found (example: 2). The count will be performed on SQL level (example: SELECT count(*) ....).
Option 2:
If you will need the records later on, you could do it like this:
$stocks = DB::table('inventories')
->whereMonth('complete_sold','=', Carbon::today()->month)
->groupBy('inventories.drug_id')
->get();
$stockCount = $stock->count();
This way $stock will have the results of the query saved (the records from the database). And $stockCount will be a number of records. The count will be performed on collection (php).

Option 1: If you only want count
$stocks = DB::table('inventories')
->whereMonth('complete_sold','=', Carbon::today()->month)
->distinct()->count("year(complete_sold)");
Option 2: If you need the records of the years
$stocks = DB::table('inventories')
->whereMonth('complete_sold','=', Carbon::today()->month)
->select(DB::raw("year(complete_sold)"))
->groupBy(DB::raw("year(complete_sold)"))->get();

Related

How to make aritmetic operations from latest record to the previous one in Laravel

I acomplished this when user make input like this:
$CalcPrevNumb = model::latest()->first();
$student = new model([
'recordtime' => $request->get('recordtime'),
'obem_sprqmo_pr_den' => $request->get('cname2') - $CalcPrevDay->cname2
]);
$student->save();
But now the goal is to do this when for example csv file is imported in to the DB(pgsql). Now I need to substract the last created and the previous one.
Something like this?
$CalcPrevNumb = model::latest()->first(); // not the latest but previous
$students = model::whereBetween('recordtime', $dateScope)
->selectRaw('recordtime, cname2 - $CalcPrevNumb')
->orderBy('recordtime', 'ASC')
->get();
How to do this in Elequent?
You may use the skip and take methods to limit the number of results returned from the query or to skip a given number of results in the query
Official Doc
Working solution:
// create object for the prev day
$user = model::whereBetween('recordtime',$dateScope)->selectRaw('recordtime')->first();
// if no records for current day don't brake the code
if($user === Null){
$user = model::latest('recordtime')->first();
}
$CalcPrevDay = model::where('recordtime', '<', $user->recordtime)
->latest('recordtime')
->first();
$students = model::whereBetween('recordtime', $dateScope)
->selectRaw('recordtime, (cname2 - ?) as obem_sprqmo_pr_den', [$CalcPrevDay->obem_m3])
->orderBy('recordtime', 'ASC')
->get();
I tryed:
$students = model::whereBetween('recordtime', $dateScope)
->selectRaw('recordtime, (cname2 - :CalcPrevDay) as obem_sprqmo_pr_den', array('CalcPrevDay'=>$CalcPrevDay->cname2))
->orderBy('recordtime', 'ASC')
->get();
But error occurred:
SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
Can't understand why but stick to my first solution. If someone have better solution will wait for it.

I want to find the value who exist in array

I have 2 queries and I would like to know the elements (date_debut) of the second query which exists in the first query (dateincal). The elements of the second query can appear one or more times in the first query.
once the dates (date debut) (dateincal) have been found i want to be able to then retrieve also the other information for the element found
$feries = Jourferie::Select('dateincal', 'description')
->where('dateincal', '>=', Carbon::now()->startOfMonth())
->where('dateincal', '<=', Carbon::now()->endOfMonth())
->get();
$plannings = Planning::Select('date_debut', 'id', 'agent_id', 'site_id')
->where('id', '!=', 0)
->where('statut', 'provisoire')
->get();
I don't know if that way can help you:
foreach($feries as $ferie){
$myresult = $plannings->contains('date_debut',$ferie->dateincal)
/* do things */
}

Retrieve data between dates in laravel

I am using an eloquent query to retrieve data from a table. The table columns look like this:
id started_at finished_at
1 06/02/2019 06/15/2019
2 06/05/2019 06/11/2019
What I want to do is, given a $date (ex: 06/08/2019 ) and get the data of the row, that the $date between started at and finished_at columns.
DB::table('table_name')->whereBetween('started_at', [$date1, $date2])
->orWhereBetween('finished_at', [$date1, $date2])->get();
$date = date('Y-m-d',strtotime($started_at));
$data = Model::where('started_at', '>', $date)->where('finished_at', '<', $date)->first();
Try this
$data = Model::where('started_at', '<', $date)->where('finished_at', '>', $date)->first();

Query max Codeigniter

I have a query that result 2 data(s) with the same employee
$this->load->library('datatables');
$this->datatables->select('a.employee_id, a.name, b.employee_position');
->from('employee a')
->join('employee_position b','b.employee_id = .a.employee_id AND `b`.`deleted`=0 AND `c`.`date_start` <= "'.$now_date.'"','inner')
$data = $this->datatables->get_adata();
$aaData = $data->aaData;
I wan't only one data appear which is choosing the max data on date_start column, how to do it?
Add these two lines in your code:
$this->db->order_by('c.date_start', 'DESC');
$this->db->limit(1, 1);

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

Resources