Order by date in different columns in Laravel - 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

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

Sorting data order by duration between start_date and end_date

I'm using Laravel 7. I have a table named assistances witch has some columns and those two columns:
- start_date
- end_date
I'd like to sort data order by the duration between start_date and end_date:
$query = Assistance::join('users', 'assistances.user_id', '=', 'users.id')
->select('assistances.*')
->orderBy('duration between start_date and end_date');
;
How can I do this ?
Please try the following - bare in mind that this is not hugely efficient
DATEDIFF returns the days between two dates. You may also want to use TIMESTAMPDIFF which accepts a first parameter such as 'hour' where you can get more granular control
$query = Assistance::join('users', 'assistances.user_id', '=', 'users.id')
->select('assistances.*')
->orderBy(DB::raw('DATEDIFF(start_date, end_date)'))
->get();
Please also use DB; in your class.

How to get Average of Tasks completed on Specific Date

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

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.

laravel queries calculating average

I want an average of all sales for the month of January in the whole table. My date_sold is in the format y/m/d. I may have many records eg 2016-01-23, 2015-01,14, 2017-01-05 all with their sales.
I want to get the average of the sales for a particular drug or drugs if they were sold in January. So far this is my controller but it is only picking the first record
$drug = $request->get('drug');
$stocks = DB::table('sales')
->join('drugs', 'drugs.id', '=', 'sales.drug_id')
->select('sales.*','drugs.name', DB::raw ('AVG(sales.quantity_sold) as average_sales'))
->whereIn('drug_id', $drug)
->whereMonth('complete_sold','=', Carbon::today()->month)
->get();
Add groupBy:
$drug = $request->get('drug');
$stocks = DB::table('sales')
->join('drugs', 'drugs.id', '=', 'sales.drug_id')
->select('sales.*','drugs.name', DB::raw ('AVG(sales.quantity_sold) as average_sales'))
->whereIn('drug_id', $drug)
->whereMonth('complete_sold','=', Carbon::today()->month)
->groupBy('drugs.id')
->get();

Resources