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();
Related
I want to fetch data from database which has booking year is 2022, and the column I have is tanggal_take whose value format is 'Y-m-d', so I want to use SELECT * FROM table_name WHERE YEAR(tanggal_take) = "2022"; How to write it in laravel controller using Eloquent?
here is my code now
$data = Booking::join('users', 'users.id', '=', 'bookings.id_member')
->join('packages', 'packages.kode_paket', '=', 'bookings.kode_paket')
->where( DB::raw('YEAR(bookings.tanggal_take)'), '=', $request->range )
->get(['bookings.*', 'packages.*', 'users.*']);
The result is 0 but in database i have 4 datas with year 2022. I also already change ->where( DB::raw('YEAR(bookings.tanggal_take)'), '=', $request->range ) to ->whereYear('bookings.tanggal_take', '=', $request->range ) but still 0 result. Any hint? I will also looking for documentation by my self rn. Thanks
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();
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
I have to search for data with given dates and time (morning and evening). How can I find available dates from data with laravel query?
If i search dates 2019-06-10 to 2019-06-18. It should only return id 3 and 4. In this query, it is returning id 1, 3 and 4. What's wrong with this query it is not checking from in between dates? I have date type column in the database for start and end date.
I want to check two conditions
Given dates should be not equal to date_from and date_to
Given dates are not between in these dates
Both conditions in same query. Hope you understand.
id event_id date_from date_to
1 1 2019-06-08 2019-06-12
2 1 2019-06-14 2019-06-16
3 2 2019-06-20 2019-06-25
4 3 2019-07-26 2019-07-27
$getEvents = Booking::where('category_id', '=', $categoryID)
->where('date_from', '!=', $startDate)
->where('date_to', '!=', $endDate)
->orWhere('date_from', '<', $startDate)
->orWhere('date_to', '>', $endDate)
->whereNotBetween('date_from', [$startDate,$endDate])
->whereNotBetween('date_to', [$startDate,$endDate])
->get();
Try this
DB::table('table_name')
->whereNotBetween('date', [star_date, end_date])
->get();
Assuming that $endDate will always be greater (or equal) than $startDate
$startDate = "2019-06-10";
$endDate = "2019-06-18";
Given dates are not between in these dates
->whereDate('date_to', '<', $startDate)
->whereDate('date_from', '>', $endDate)
If given $startDate is greater than db 'date_to', $startDate is not between 'date_to' and 'date_from'. Will retrieve records with 'date_to' less than "2019-06-10".
And when the given 'date_from' is greater than $endDate, $endDate is not between 'date_to' and 'date_from'. Will retrieve records with 'date_from' greater than "2019-06-18".
Given dates should be not equal to date_from and date_to
This is not needed since in the query of point 2, we use > and <, which will not take into account equals.
//->where('date_from', '<>', $startDate)->where('date_from', '<>', $endDate)
//->where('date_to', '<>', $startDate)->where('date_to', '<>', $endDate)
Just:
$getEvents = Booking::where('category_id', '=', $categoryID)
->whereDate('date_to', '<', $startDate)
->whereDate('date_from', '>', $endDate)
->get();
Laravel 5
$getEvents = Booking::where('category_id', '=', $categoryID)
->where('date_from', '<', $startDate)
->where('date_to', '>', $endDate)
->get();
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.