Laravel使用数据表的数据

时间:2015-01-11 20:37:58

标签: php laravel datatables

我有一个UsesController,目前看起来如下所示,我试图弄清楚什么是我处理数据中所有大量数据的最佳方式。到目前为止,我只是从我的数据库中检索所有用户并将其传递给我的视图,然后在foreach循环中循环数据,在我的HTML表中创建一个新的表行。然后在我的javascript初始化表作为数据库表。走这条路是否有任何不利之处,或者我应该把它变成各种各样的API,或者什么样的建议对我有帮助。

<?php

use MyApp\Services\UserCreatorService;

class UsersController extends BaseController {

    protected $userCreator;

    public function __construct(UserCreatorService $userCreator)
    {
        parent::__construct();
        $this->userCreator = $userCreator;
        $this->beforeFilter('role:Administrator.Owner');
    }

    /**
    * Display a listing of users
    *
    * @return Response
    */
    public function index()
    {
        // Retrieve all users from database with roles and statuses
        $users = User::with('role')->with('status')->get();

        // Return a view to display all users by passing users variable to view.
        return View::make('users.index', compact('users'));
    }
}

<table class="table table-striped table-bordered responsive resourceTable">
    <thead class="">
        <tr>
            <th class="center">ID</th>
            <th>Name</th>
            <th>Email Address</th>
            <th>Username</th>
            <th>Role</th>
            <th>Status</th>
            <th class="nosortable center">Actions</th>
        </tr>
    </thead>

    <tbody>
        @foreach($users as $user)
            <tr>
                <td class="center">{{ $user->id }}</td>
                <td>{{ $user->getFullName() }}</td>
                <td>{{ $user->email_address }}</td>
                <td>{{ $user->username }}</td>
                <td>{{ $user->role->role_name }}</td>
                <td>{{ $user->status->status_name }}</td>
                <td class="center">
                    <!-- TODO: Figure out how to make a function with actions td.-->
                    @if ( $user->role['id'] < $currentUser->role['id'] )
                        <a data-original-title="Edit" href="{{ route('users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
                        <a data-original-title="Delete" href="{{ route('users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips ajax-delete"><i class="fa fa-trash-o"></i></a>
                    @endif
                    <a data-original-title="Profile" href="{{ route('users.show', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-eye"></i></a>
                </td>
            </tr>
        @endforeach
    </tbody>
</table>

// Set defaults for all resource tables.
$.extend( $.fn.dataTable.defaults, {
    "aoColumnDefs": [
        { "bSortable": false, "aTargets": [ "nosortable" ] }
    ],
    "pagingType": "full_numbers"
});

$(document).ready(function() {

    //we define the table in a global variable so we can later manipulate it...
    resourceTable = $('.resourceTable').dataTable();

    var $addButton = $('<button class="btn btn-primary myActionButtons" id="addNew">Add New</button>');

    /* TODO: Find out if this is the best place for a add new button globally on table pages. */
    $('.dataTables_filter').parent().append($addButton);
});

$(document).on('click', '#addNew', function(e) {
    e.preventDefault();
    window.location.replace(window.location.href + '/create');
});

2 个答案:

答案 0 :(得分:1)

嗯,你的方法没有错。但实际上,我建议您可以将大数据分成几部分。它将显着提高性能和速度。

在分页时,使用Laravel代替js或jQuery。你可以自己试验一下,并会在Laravel中看到性能提升。

答案 1 :(得分:0)

对于大量数据 API 是更好的解决方案。和杰弗里解释了为什么不良做法使用$users = User::with('role')->with('status')->get(); 点击此链接,它将为您提供帮助LINK