用ajax显示的简单方法?

I have 6 functions in controllers to order by ASC and DESC 3fields.

Functions:

 public function orderByIdAsc(){
        $projects = DB::table('projects')->orderBy('id','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 

    }
    public function orderByIdDesc(){
        $projects = DB::table('projects')->orderBy('id','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByOrderAsc(){
        $projects = DB::table('projects')->orderBy('order','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByOrderDesc(){
        $projects = DB::table('projects')->orderBy('order','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByPublicAsc(){
        $projects = DB::table('projects')->orderBy('public','asc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    }
    public function orderByPublicDesc(){
        $projects = DB::table('projects')->orderBy('public','desc')->get();
        return view('cms.public.views.projects.menu', ['projects' => $projects]); 
    } 

With her 6 routes too:

Route::prefix('admin')->group(function () {

    Route::get('project/orderByIdAsc', ['uses' => 'AdminController@orderByIdAsc', 'as' => 'admin.projects.orderByIdAsc']);
        Route::get('project/orderByIdDesc', ['uses' => 'AdminController@orderByIdDesc', 'as' => 'admin.projects.orderByIdDesc']);
        Route::get('project/orderByOrderAsc', ['uses' => 'AdminController@orderByOrderAsc', 'as' => 'admin.projects.orderByOrderAsc']);
        Route::get('project/orderByOrderDesc', ['uses' => 'AdminController@orderByOrderDesc', 'as' => 'admin.projects.orderByOrderDesc']);
        Route::get('project/orderByPublicAsc', ['uses' => 'AdminController@orderByPublicAsc', 'as' => 'admin.projects.orderByPublicAsc']);
        Route::get('project/orderByPublicDesc', ['uses' => 'AdminController@orderByPublicDesc', 'as' => 'admin.projects.orderByPublicDesc']);
});

For the moment i call to the functions here:

<th><a href="{{ route('admin.projects.orderByIdAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByIdAsc"></span></a>Id<a href="{{ route('admin.projects.orderByIdDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByIdDesc"></span></a></th>
<th>Slug</th>
<th><a href="{{ route('admin.projects.orderByOrderAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByOrderAsc"></span></a>Order<a href="{{ route('admin.projects.orderByOrderDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByOrderDesc"></span></a></th>
<th><a href="{{ route('admin.projects.orderByPublicAsc')}}"><span class="glyphicon glyphicon-arrow-up" id="orderByPublicAsc"></span></a>Public<a href="{{ route('admin.projects.orderByPublicDesc')}}"><span class="glyphicon glyphicon-arrow-down" id="orderByPublicDesc"></span></a></th>

Any way to do it with ajax easier than make 6 divs and hide / display it?

Thanks a lot!

For this case you have two choices :

  • First one : using path parameters

You can combine all the routes of ordering in one :

Route::prefix('admin')->group(function () {

    Route::get('project/order/{field}/{order}', [
                'uses' => 'AdminController@order', 
                'as' => 'admin.projects.order'
            ]);

});

In your AdminController :

public function order($field, $order) 
{
    $projects = DB::table('projects')->orderBy($field,$order)->get();
    return view('cms.public.views.projects.menu') 
               ->withProjects($projects);
}

In the view :

<a href="{{ route('admin.projects.order', ['field' => 'id', 'order' => 'asc']) }}">Order by ID</a>
  • Second one : using query strings

For the route :

Route::prefix('admin')->group(function () {

    Route::get('project/order', [
                'uses' => 'AdminController@order', 
                'as' => 'admin.projects.order'
            ]);

});

In your AdminController :

public function order(\Illuminate\Http\Request $request) 
{
    $order = $request->query('order');
    $field = $request->query('field');
    $projects = DB::table('projects')->orderBy($field,$order)->get();
    return view('cms.public.views.projects.menu') 
               ->withProjects($projects);
}

And in the view :

<a href="{{route('admin.projects.order', ['field' => 'id', 'order' => 'desc'])}}">Order by ID</a>