I have search result page for a car seller website, it a listing page. There are some options on html template like:
order by data , price , price des
I should order results by clicking this options. Client doesn't want to use datatables. How can I implement it? I know how to fetch data with laravel:
DB::table('<table name>')->orderBy('<field name>', '<direction>')->get();
But I couldn't understand the logic, because after I come to search result page with data, how can I take query and change the order at current page?
Or is there a technique for that?
You need to send the column name and direction to the controller. And in the controller you get the column, direction and put them into orderBy function.
The link in your view should look something right this
http://www.yoururl.com/something?column=date&direction=asc
$sortable_fields = array("date", "price");
/* set the default the column sort field to date if there is no value was passed */
$column = in_array(Input::get('column'), $searchable_fields) ? Input::get('column') : 'date'
/* set the default sort direction to desc if there is no value was passed */
$direction = (Input::get('direction') == 'asc') ? 'asc' : 'desc';
/* Use where function for searching before do the order by */
$query = DB::table('<table name>') ;
if (Input::get('title')) {
$query->where('title', Input::get('title');
}
DB::table('<table name>')->orderBy($column, $direction)->get();
As you don't want to do the operation in datatables. Then you should do a ajax call to get that result in the any action such as on change or onpress event or anything that you required.
In laravel :
You need to make your orderBy
such that
DB::table('users')->orderBy('name', 'desc')->get();
For more info about selects