How to get Average of Tasks completed on Specific Date - laravel

I have Task model it contain task_status column and due_date column. I want to get average of each date between date array.
Task::whereIn('due_date', $dates)
->where('task_status',$status)
->where('user_id', \Auth::id())
->whereRAW('YEAR(due_date) =?', Carbon::now()->format('Y'))
->groupBy('due_date')
->orderBy('due_date', 'DESC')
->get(array(
DB::raw('Date(due_date) as date'),
DB::raw('COUNT(*) as "count"')
));
status is complete or incomplete

Related

How to get data which has expiry date nearest to current date?

I have PurchaseOrderProduct Model from which I am getting data like that
PurchaseOrderProduct::with('products')
->with('warehouse_locations')
->with('productStatuses')
->with('purchase_orders')
->where('product_id', $request->product_id)
->where('free_qty', '>=', 1)
->get();
Now in this table, I have the column expiry_date of type date i want to fetch data orderBy expiry_date nearest to the current date .
Means purchase_order_product whose expiry date is nearest to current date should come first.
$product = PurchaseOrderProduct::with('products')
->with('warehouse_locations')
->with('productStatuses')
->with('purchase_orders')
->where('product_id', $request->product_id)
->where('free_qty', '>=', 1)
->whereDate('expiry_date', '>', Carbon::now())
->orderBy('expiry_date','asc')
->get();

Fetch records that matches month and year using Laravel eloquent

How do I query a table such that it only fetches records that matches a particular month and year e.g. Y-m i.e. 2020-10. of the 'created_at' column.
This is what I am currently doing:
School::where('user_id', 1)->where('created_at', '=', '2020-10')->count();
I would suggest to create a date range for your filter so this way your query will remain sargable
$date = \Carbon\Carbon::parse($year."-".$month."-01");
$start = $date->startOfMonth()->format('Y-m-d H:i:s');
$end = $date->endOfMonth()->format('Y-m-d H:i:s');
School::where('user_id', 1)
->whereBetween('created_at', [$start, $end])
->count();

Order by date in different columns in Laravel

I can't seem to get how to order by month in Laravel 7. I use different columns for my month, day, year and time and instead of ordering it by month, it orders the months alphabetically.
Intended Result: January, February, March, April
Actual Result: April, February, January, March
This is my code:
$ledgers = Ledger::orderBy('month', 'DESC')
->orderBy('day', 'DESC')
->orderBy('year', 'DESC')
->orderBy('time', 'DESC')
->where('user_id', auth()->user()->id)
->paginate(8);
first of all: i must say that you should store your date info in database in datetime column like Tim Lewis said in comment ...
any way
you can use order by fields , this kind of order make the result ordered according to the fields you provide ...
$ledgers = Ledger::orderByRaw('FIELD(month,'January','February','March','May', 'June','July','August','September','October','November','December')')
->orderBy('day', 'DESC')
->orderBy('year', 'DESC')
->orderBy('time', 'DESC')
->where('user_id', auth()->user()->id)
->paginate(8);
please see:
https://stackoverflow.com/a/9378709/10573560

Laravel get just updated rows in date range - using WhereBetween and Where in query

I have jobs table in the database with the default columns created_at and updated_at.
I want to return the number of jobs created and the number updated.
However as the updated_at column is populated at the point of creation, I want to exclude any jobs where the updated_at value is the same as the created_at value (in order to get just the jobs that have been updated since being created).
I use the following code to get the number of updated jobs between a time range.
$jobsUpdated = Job::where('id', $id)
->whereBetween('updated_at', [$value['fromDate'], $value['tillDate']])
->count();
How can I add another where clause to exclude those results which have the same value as created_at.
In theory something like:
->andWhere('updated_at', '!=' , 'created_at')
But andWhere does not exist, so this is just to show my thinking.
Thanks!
$jobsUpdated = Job::where('id', $id)
->whereBetween('updated_at', [$value['fromDate'], $value['tillDate']])
->whereColumn('updated_at', '!=' , 'created_at')
->count();
to compare column value use whereColumn() function referenced by official doc

Laravel join two tables with where that compares two date time columns

I have two tables that I want to run joint query on using the value of two date time columns, I have products table and sync_status tables, I want to return all products with updated_at date time greater than synced_at date time.
DB::table('products')
->join('sync_statuses', function ($join) {
$join->on('products.product_number', '=', 'sync_statuses.product_number')
->where('products.updated_at', '>', 'sync_statuses.synced_at')
->whereNull('products.deleted_at');
})
->select('products.product_number');
This SQL represents what I am trying to achieve using Eloquent:
SELECT products.product_number
FROM products
JOIN push_statuses
ON products.product_number = statuses.product_number
AND (
statuses.synced_at IS NULL
OR products.updated_at > statuses.synced_at
)
You have to use on() instead of where() to compare columns:
->on('products.updated_at', '>', 'sync_statuses.synced_at')
This worked for me:
DB::table('products')
->join('statuses', function ($join) {
$join->on('products.product_number', '=', 'statuses.product_number')
->where(DB::raw('products.updated_at'), '>=', DB::raw('statuses.synced_at'))
->whereNull('products.deleted_at');
})->select('products.product_number');
I needed to use DB::raw('products.updated_at') to reference each date time column in the where() clause.

Resources