Bruce Gust
asked on
How can I sort by date in Laravel 6?
Here's my Controller code:
When I remove the "sortBy" dynamic, all is well. When I include the "sortBy," I get this:
asort() expects parameter 2 to be int, string given
My datatype in my database is "date," so what do I need to change so I can sort my rows by date?
I'm running Laravel 6.
public function index()
{
$events = Calendar::all()->sortBy('start_date', 'desc');
return view('admin/listCalendar', compact('events'));
}
When I remove the "sortBy" dynamic, all is well. When I include the "sortBy," I get this:
asort() expects parameter 2 to be int, string given
My datatype in my database is "date," so what do I need to change so I can sort my rows by date?
I'm running Laravel 6.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Chris, I was able to get it to work by using something I had done previously. The "winner" was this:
public function events() {
$events = Calendar::sortable()->orderBy('start_date', 'asc')->paginate(10);
return view('events', compact('events'));
}
What I want to know is, "What's the significance" of "->get?" I figure there's more than one way to skin a cat, but that piece of syntax intrigued me. What is that and why does it work?
As always, thanks for your help.
I've got another question about being able to email a form. Stay tuned...
public function events() {
$events = Calendar::sortable()->orderBy('start_date', 'asc')->paginate(10);
return view('events', compact('events'));
}
What I want to know is, "What's the significance" of "->get?" I figure there's more than one way to skin a cat, but that piece of syntax intrigued me. What is that and why does it work?
As always, thanks for your help.
I've got another question about being able to email a form. Stay tuned...
Hey Bruce,
Glad you got it working, although I'm not sure why your need the sortable() method - it should work just fine without it.
As for the get() - think of like an instruction to execute a query. In Laravel, you work with a Query Builder. This allows you to build an query in a fluent manner, bit by bit, and then when you're done, you execute the query:
Glad you got it working, although I'm not sure why your need the sortable() method - it should work just fine without it.
As for the get() - think of like an instruction to execute a query. In Laravel, you work with a Query Builder. This allows you to build an query in a fluent manner, bit by bit, and then when you're done, you execute the query:
$builder = MyModel::select('name')
->where('id', 5)
->orderBy('name', 'asc');
if ($limit) {
$builder->take(10);
}
$results = $builder->get();
The above builds the query with a select, where and orderby clause. It then conditionally adds a LIMIT. Nothing is sent to the DB until we call get(). That's the instruction to execute the query. There are various other methods that also fire the execution, such as first(), last() etc. Before you fire get(), you have an instance of a Query Builder. After you fire get() you have a collection of Models.
For example: https://github.com/laravel/ideas/issues/11
Are you trying to sort by date and then by description? It appears that SortBy doesn't allow that, but can be cascaded as shown in the link above.