Searching, sorting and paging not working for datatable laravel

So I’m trying to use the datatable as seen in my code written below. Unfortunately for some unknown reasons the functions for sorting, searching and paging does not work. May I know what did I missed out or the reason for it not working?

@extends('app')
@section('header')
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="nofollow noreferrer noopener" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" rel="nofollow noreferrer noopener" rel="stylesheet">
@stop
@section('content')

<h1 class="page-header">View Log Information</h1>
<br/>

<div class="container">
    <div class="col-xs-12 col-md-12" style="overflow:auto">
    <table class="table table-hover" id="logTable">
        <thead>
        <tr>
            <th>
                User
            </th>

            <th>
                Log Description
            </th>
            <th>
                Time Stamp
            </th>
        </tr>
        </thead>
        @foreach ($logs as $log)
        <tbody>
            <tr>
                <td>
                    {{ AppUser::find($log->user_id)->name }}
                </td>

                <td>
                    {{ $log->log_description }}
                </td>
                <td>
                    {{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
                </td>
            </tr>
        </tbody>
        @endforeach
    </table>
    </div>
</div>

@stop

@section('scripts')
    <!-- Bootstrap Based Data Table Plugin Script-->
    <script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function() {
        $('#logTable').DataTable();
    } );
    </script>
@stop

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

try this:

<table class="table table-hover" id="logTable">
    <thead>
    <tr>
        <th>
            User
        </th>

        <th>
            Log Description
        </th>
        <th>
            Time Stamp
        </th>
    </tr>
    </thead>

    <tbody>
        @foreach ($logs as $log)
        <tr>
            <td>
                {{ AppUser::find($log->user_id)->name }}
            </td>

            <td>
                {{ $log->log_description }}
            </td>
            <td>
                {{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
            </td>
        </tr>
        @endforeach
    </tbody> 
</table>

Move the @foreach statement under tbody. It’s not working because you keep duplicating the tbody instead of <tr> inside the <tbody>.

And this is just a suggestion. Don’t do the query at view. it’s not a good practice. Do all the query inside controller. You can use Laravel Relationship for AppUser::find($log->user_id)->name.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x