It should sort the records by the highest levels of urgency by the oldest dates, but it returns the following results:
2022-01-02 - Emergency.
1978-05-21 - Very urgent.
2022-02-19 - Urgent.
13- 09-1990 - Little urgent.
09-08-2022 - Not urgent.
FilaPedido::orderBy('emergency_level', 'DESC')
->orderBy('data_request', 'ASC')
->paginate(10);
The data doesn't seem to match the ordering of the dates, how do I resolve this?
Related
I am making a schedule display system and it shows the schedules of ferries. my problem is if I dont put midnight time in order then query skips midnight time and displays morning instead
EXAMPLE:-
if i put data in order like this
1:30 am
6:30 am
7:30 am
then the times show correctly, but if its not in order
6:30 am
7:30 am
1:30 am
then the query skips the 1:30 am and goes straight to 6:30 to display.
Here is my laravel SQL Query:- if departure time is greater than current time then show the next available one
DB::table('ferries')->groupBy('ferry_name')
->whereTime('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name', $terminal_name)->get()->all();
Resukltsets are by nature unsorted yo you need to add a sorting order
This would sort ascending see manual
DB::table('ferries')->groupBy('ferry_name')
->whereTime('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name',$terminal_name)->orderBy('departure_time')->get()->all()
Used unique() instead of groupBy() then the orderBy worked not sure why.
$posts = DB::table('ferries')->orderBy('departure_time', 'asc')
->where('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name', $terminal_name)->get()->unique('ferry_name')->all();
I've got a Laravel 8 application whereby I need to count the number of total applications for each day and output a count of the data in the most efficient way, for instance, 500 applications on 1st, 1,000 applications on 2nd and so fourth, depending on how many applications there are each day.
I'm using DB::raw() to count my rows, however, Laravel's timestamps by default includes the hours, minutes and seconds as part of the date, and so this isn't going to work for me, I need to format the date to exclude this and just contain the day.
My query doesn't seem to be returning anything, and no error either, so I think there's something wrong with my first DB::raw(), it doesn't seem to be formatted the created_at column at all, what am I missing?
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT("created_at, %Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupBy('created_at')
->get();
You can use groupByRaw()
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupByRaw('DATE_FORMAT(created_at, "%Y-%m-%d")')
->get();
Also, you can use selectRaw() instead of select(DB::raw)
This is some sorting:
Get 20 posts sorted by "views".
AFTER IT FINISH, then go to the next step.
sort it by "created_at".
How to do it?
Heres my current code (which work, but I dont want to use SORTBY) for some reason sortby is not working for other project. if possible i want it to be as one eloquent query:
$this->data['today_post'] = Posts::orderBy('views', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
$this->data['today_post'] = $this->data['today_post']->sortByDesc('created_at');
This code is not working, because LIMIT is usually applied as the last operation, so the result will first be sorted by "views" and "created_at" and then limited to 20. I dont want that. I want to sort and limit then after all is complete. I want to sort again the last time.
$this->data['today_post'] = Posts::orderBy('views', 'desc')->orderBy('created_at', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
Thank you so much
You'll still need to use sortBy() or sortByDesc() if you want to use Eloquent. I've just tested this solution and it works perfectly:
Posts::orderBy('views', 'desc')
->where('created_at', '>=', Carbon::now()->subDay())
->take(20)
->get()
->sortByDesc('created_at');
Please help guys. I have my database:
Moreless...
I want to build a query that will count each column with a specific date for a specific page. For example Page 1 have visits 25th Dec and 26th Dec, but I want count every visit on 26th.
Somebody could help me even with pure MySQL?
This query will give you how many visitors does a specific page has on specific date. I am not sure if this is what you aim for but that's what I understood from the question.
SELECT DATE(created_at) AS date_visited, page_id, COUNT(1) AS visits
FROM your_table_name
GROUP BY DATE(created_at), page_id
As explanation - it is a basic GROUP BY usage, you can refer to the MySQL documentation http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html if you are not familiar with these. It has nothing to do with Laravel tho.
You can do it using whereDate and groupBy as:
Model::select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();
Or you can also use query builder as:
DB::table('table_name')
->select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();
Scenario:
The startdate and enddata in the database is 2015-07-20 and 2015-07-30 respectively and the query that works properly is,
Model::whereBetween('startdate',array('2015-07-30','2015-08-10'))->get();
The query return 1 record from the table which is the expected result. Now what I expect is a slight change. The query should not retrieve the record by considering the date 2015-07-30 does not fall in between 2015-07-30. How do I achieve this?
whereBetween is inclusive. In order to exclude one of the edges you'll need to build a between query manually:
Model::where('startdate', '>', '2015-07-30')->where('startdate', '<=', '2015-08-10')->get();
Easy Solution
Model::whereBetween(DB::raw('date(startdate)'),array('2015-07-30','2015-08-10'))->get();