Laravel + Datatables,如何将id传递给控制器 ?

问题描述 投票:2回答:1

我想在我的Laravel项目中开始使用Datatables,所以我遵循了本教程:https://youtu.be/ejj-078OvfY

它运行良好,但我无法弄清楚如何将参数传递给我的控制器,因为该路由是通过视图上的JavaScript函数通过AJAX调用调用的。

如果您不熟悉本教程,可能听起来有点奇怪,所以让我告诉您如何设置:

路线:

Route::get('/client/{id}', array('before' => 'auth', 'as' => 'getClient', 'uses' => 'ClientsController@getClient'));
Route::get('getAllParticipants', array('before' => 'auth', 'as' => 'getAllParticipants', 'uses' => 'ClientsController@showAllParticipants'));

控制器:

public function getClient() {
  return View::make('/forms/dashboard_clients');
 }
 public function showAllParticipants () {
        $allParticipants = User::where('users.id', '=', $id) //I need the ID parameter here
            ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
            ->where('users_roles.role_id', '!=', Role::USER_PARTICIPANT)
            ->groupBy('users.id')
            ->get();

        return Datatable::collection($allParticipants)
            ->searchColumns('firstname', 'lastname', 'email')
            ->orderColumns('firstname', 'lastname', 'email')
            ->addColumn('firstname', function ($model) {
                return $model->firstname;
            })
            ->addColumn('lastname', function ($model) {
                return $model->lastname;
            })
            ->addColumn('email', function ($model) {
                return $model->email;
            })
        ->make();
 }

视图:

<div class="row">    
    <div class="col-md-12">
        <table id="allParticipants" class="table table-striped table-hover">
          <thead>
            <tr>
              <th scope="col">@lang('table.headers.fname')</th>
              <th scope="col">@lang('table.headers.lname')</th>
              <th scope="col">@lang('table.headers.email')</th>
              <th scope="col">@lang('table.headers.action')</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td></td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    var allParticipants=null;
    $(document).ready(function(){
        allParticipants = $('#allParticipants').dataTable({
            "ajax": "/getAllParticipants",
            "lengthMenu": [[10, 50, 100, -1], [10, 50, 100, "All"]]
        });
    });
</script>

总而言之,用户登陆到打印视图的/ client / {id}路径。从该视图中,JavaScript通过其id标识表,并发送一个Ajax调用,该调用触发getAllParticipants路由,其中​​一组参与者被发送到视图。

关于如何在控制器上为showAllParticipants函数指定ID参数的任何想法?

laravel datatables
1个回答
0
投票

从我可以看到你想要能够在路由上设置ID参数,这很简单,问题是Datatables如何发出AJAX请求,它是否在发出“getAllParticipants”调用时发送ID?

如果是这样,您可以通过两种方式进行操作,或者像在客户端路径上那样在该路径上设置ID。或者您使用传统的GET参数并通过在控制器中调用$ request-> input('paramname')来获取它。

令我困惑的是,您的Datatable不发送任何数据,只是调用Controller路由而不发送任何数据。要发送数据,我相信它应该是这样的

$('#example').dataTable( {
  "ajax": {
    "url": "/getAllParticipants",
    "data": {
        "id": 451
    }
  }
} );

或者

$('#example').dataTable( {
  "ajax": {
    "url": "/getAllParticipants?id=" + 451
  }
} );

这样在我的控制器中我会注入Request类并从中获取“id”

 public function showAllParticipants (Request $request) {
        $id = $request->input('id');

        $allParticipants = User::where('users.id', '=', $id) //I need the ID parameter here
            ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
            ->where('users_roles.role_id', '!=', Role::USER_PARTICIPANT)
            ->groupBy('users.id')
            ->get();

        return Datatable::collection($allParticipants)
            ->searchColumns('firstname', 'lastname', 'email')
            ->orderColumns('firstname', 'lastname', 'email')
            ->addColumn('firstname', function ($model) {
                return $model->firstname;
            })
            ->addColumn('lastname', function ($model) {
                return $model->lastname;
            })
            ->addColumn('email', function ($model) {
                return $model->email;
            })
        ->make();
 }
© www.soinside.com 2019 - 2024. All rights reserved.