Laravel为数据表处理数据


Laravel working with data for 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');
});

好吧,你的方法没有错。但在实践中,我建议你可以将大数据分页成碎片。它将大大提高性能和速度。

在分页方面,请使用Laravel而不是js或jQuery。您可以自己进行实验,并将在 Laravel 中看到分页的性能提升。

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