【问题标题】:How to pass route with {id} to Datatable blade. To view specific user details如何将带有 {id} 的路由传递给 Datatable 刀片。查看特定用户详细信息
【发布时间】:2018-02-07 10:38:06
【问题描述】:

这是我的路线

Route::get('/view/{id}', [
    'uses' => 'ClientController@view',
    'as' => 'view'
]);

这是我的客户端控制器

public function view($id){
     $client = Client::where('id',$id)->first();
     return view('view',compact('client'));
}

这是我的数据表

@extends('layouts.datatables_master')

@section('content')
    <table class="table table-bordered" id="clients-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Company Name</th>
                <th>Name</th>
                <th>Surname</th>
                <th>ID Number</th>
                <th>Created</th>
                <th>Last Updated</th>
                <th>Actions</th>
            </tr>
        </thead>
    </table>
@stop


@push('scripts')
<script>
$(function() {
    $('#clients-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '{!! route('business.list') !!}',
        columns: [
            { data: 'id', name: 'id' },
            { data: 'companyname', name: 'companyname' },
            { data: 'name', name: 'name' },
            { data: 'surname', name: 'surname' },
            { data: 'idnumber', name: 'idnumber' },
            { data: 'created_at', name: 'created_at' },
            { data: 'updated_at', name: 'updated_at' },
            { data: "actions",
                      "render": function(data, type, row) {
                        return '<a href="{{route('view',1)}}" class="btn btn-xs btn-info"><i class="fa fa-search" title="View"></i></a>                                               <a href="#" class="btn btn-xs btn-primary"><i class="fa fa-pencil" data-toggle="tooltip" data-placement="top" title="Edit"></i></a>    <a href="#" class="btn btn-xs btn-danger"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i></a>'

                               ;}
            }

        ]
    });
});
</script>
@endpush

问题

我的主要问题是为所有用户传递一个 ID。如果您在我的 DataTable 上检查我的 render 函数,我说 return route(view,1) 是 1。我想通过 $id 但它说未定义的变量。请帮忙。

【问题讨论】:

  • 你可以像 /view/1 这样写。它会解决你的问题

标签: php laravel datatables


【解决方案1】:

您可以从控制器返回生成的路由链接,例如:

return datatables(Client::query())
        ->addColumn('actions', function ($client) {
            return [
                'edit_link' => route('view', [$id => $client->id]),
            ];
        })->toJson();

并在您的视图中呈现为

{
    data: "actions",
    render: function(data, type, row) {
        return 
        '<a href="' + data.edit_link + '" class="btn btn-xs btn-info">' + 
        '<i class="fa fa-search" title="View"></i></a>' +
        '<a href="#" class="btn btn-xs btn-primary">' +
        '<i class="fa fa-pencil" data-toggle="tooltip" data-placement="top" title="Edit"></i>' +
        '</a>' +
        '<a href="#" class="btn btn-xs btn-danger">' +
        '<i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="Delete"></i>' +
        '</a>';
    }
}
【解决方案2】:

data 的值应该是一个 'id',并且使用 url() 而不是 route() :

{ data: "id",
    "render": function(data, type, row) {
       return '<a href="{{ url("view") }}/"'+ data +'>View</a>';
        }
}

【讨论】:

    【解决方案3】:

    这样试试

     return   '<a href= "view/' + 1 +'" class="btn btn-xs btn-info"><i class="fa fa-search" title="View"></i></a> 
    

    【讨论】:

      【解决方案4】:

      您将客户端变量传递给刀片

      {{ route('view',$client->id); }}
      

      【讨论】:

      • 你的方法说未定义的变量客户端
      猜你喜欢
      • 2021-08-25
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多