Laravel + Datatables,如何将 id 传递给 Controller ​​?

标签 laravel datatables

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

它运行良好,但我不知道如何将参数传递给我的 Controller ,因为该路由是由 AJAX 调用通过 View 上的 JavaScript 函数调用的。

如果您不熟悉教程,这听起来可能有点奇怪,所以让我向您展示这是如何设置的:

路线:

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

Controller :

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} 打印 View 的路由。在该 View 中,JavaScript 通过其 ID 识别表,并发送一个 Ajax 调用,该调用触发 getAllParticipants 路由,参与者的集合被发送到该 View 。

知道如何为 Controller 上的 showAllParticipants 函数指定 ID 参数吗?

最佳答案

据我所知,您希望能够在路由上设置 ID 参数,这很简单,问题是 Datatables 如何发出 AJAX 请求,它在发出“getAllParticipants”调用时是否发送 ID?

如果是这样,您可以通过两种方式进行处理,要么像为客户端路由那样在该路由上设置一个 ID。或者您使用传统的 GET 参数并通过在 Controller 中调用 $request->input('paramname') 来获取它。

让我感到困惑的是,您的 Datatable 没有发送任何数据,它只是调用 Controller 路由而不发送任何数据。 要发送数据,我认为应该像这样

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

或者替代地

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

在我的 Controller 中,我将通过这种方式注入(inject) 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();
 }

关于Laravel + Datatables,如何将 id 传递给 Controller ​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47870782/

相关文章:

php - jquery数据表中的Group Concat方法

php - 我可以安全地将 Composer 创建的 Laravel 项目移动到另一个目录吗?

php - 具有多个模型的 Jwt

javascript - Laravel Mix 和 Webpack : Cannot Load JSON File (Webpack Adding . js 到文件名)

javascript - Laravel 5,4 JavaScript 函数与 ajax

php - Laravel 多对多查询关系与 where 子句

c# - 使用 C# MVC BeginForm 发布表单字段数组

javascript - 使用 jQuery DataTables 时禁用对最后一列的排序

jquery - angularjs数据表无法重新初始化表错误

javascript - 为什么这个数据表不根据我的搜索过滤器过滤数据?