如何在Laravel DataTables中进行路由 [英] How to route in Laravel DataTables

查看:69
本文介绍了如何在Laravel DataTables中进行路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel DataTables的新手,我试图在数据表中插入控制器,但出现异常.

I am new to Laravel DataTables and i am trying to insert my controller in the datatables but i am getting an exception.

如何完成此任务?

PS:当我使用user/items/'$users->id'/control之类的常规路由时,它可以正常工作,但我想使用UserController@edit, $user->id

PS: When I use the normal route like user/items/'$users->id'/control, it works fine but I want to use the the route like UserController@edit, $user->id

public function getPost()
{
    $users= User::where('id', Auth::user()->id);
    return Datatables::of($users)->addColumn('action', function ($user) {
        return '<a href="{ action('UserController@edit', $user->id)}" title="edit" ><i class="fa fa-edit"></i></a>
                &nbsp;&nbsp;';

    })->make(true);     

}

推荐答案

您的代码中有一些提及:

Some mentions to your code:

// this will get you only one user
$users = User::where('id', Auth::user()->id);

// therefore it does not make sense to show one user in a table, does it ;-)
return Datatables::of($users)->make(true);     

对您的问题:

您需要执行两项操作:一项用于呈现视图,另一项用于通过Ajax获取数据表的数据(如果使用ServerSide呈现)

You need two actions: One to render the view and one to fetch the data for the datatables via Ajax (if you use ServerSide rendering)

假设您要显示用户列表:

Assuming you want to show a list of users:

Route::get('users', 'UserController@index');
Route::get('users-dt', 'UserController@indexDataTables')->name('users:dt');

控制器

public function index()
{
    return view('users.index');
}

public function indexDataTables()
{
    $users = User::query();

    return DataTables::eloquent($users)->toJson();
}

user/index.blade.php

<script>
    $(function() {
        $('#users-table').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{!! route('users:dt') !!}',
            columns: [
                { data: 'name', name: 'name'},
            ],
        });
    })
</script>

这篇关于如何在Laravel DataTables中进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆