0

I have a UsesController that currently looks like the following and I'm trying to figure out what is the best way for me to handle all my large amounts of data in my data. As of right now I am simply retrieving all of the users from my database and passing it to my view and and then looping over the data in a foreach loop creating a new table row in my HTML table. And then in my javascript initializing the table as a database table. Is there any disadvantages of going this route or should I turn this into an API of sorts or what kind of suggestions would help me.

<?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 Answers 2

0

Well, there is nothing wrong with your approach. But in practice, I suggest that you may paginate big data into pieces. It will dramatically improve performance and speed.

When it comes to pagination, do it with Laravel instead of js or jQuery. You can experiment yourself and will see the boost of performance with pagination in Laravel.

Sign up to request clarification or add additional context in comments.

Comments

0

For the Large amount data API is better solution. and Jeffry explanied why bad practice use $users = User::with('role')->with('status')->get(); Check this link it will help you LINK

2 Comments

But I don't understand why I need an API for my own app when I just want to get data from my database and send it to the view.
Jeffry has clearly explained in that video series why we not use for other method Ex: get(), all() its bad practice for large amount of data. just pay 9$ check those video tutorial it sure wil help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.