搜索、排序和分页不适用于数据表laravel



所以我正在尝试使用下面编写的代码中的数据表。不幸的是,由于一些未知的原因,排序、搜索和分页功能无法工作。我可以知道我错过了什么吗?或者它不起作用的原因是什么?

@extends('app')
@section('header')
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" 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="http://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

试试这个:

<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>

@foreach语句移到tbody下。它不起作用,因为您一直在<tbody>中复制tbody而不是<tr>

这只是一个建议。不要在视图中执行query。这不是一个好的做法。在控制器内执行所有查询。您可以将Laravel关系用于AppUser::find($log->user_id)->name

最新更新