简体   繁体   中英

laravel 5.6 datatable pagination not working

I'm using laravel 5.6 and try to add pagination to bootstrap datatable. But it's not working.I got error.Please help me to find mistake I did.

Error

Method Illuminate\\Database\\Eloquent\\Collection::links does not exist.

designation.blade.php(View)

 @if(count($designations)>=1)
    <table class="table table-dark" id="designationTable">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Designation Type</th>
            <th scope="col">Status</th>
            <th scope="col">Create At</th>
            <th scope="col">Action</th>

        </tr>
        </thead>
        @foreach ($designations as $designation)
        <tbody>
        <tr>
            <th scope="row">{{$designation->id}}</th>
            <td>{{$designation->designation_type}}</td>
            <td>{{$designation->status}}</td>
            <td>{{$designation->created_at}}</td>
            <td>
              <button type="" class="btn btn-secondary" id="btn_update_designation">Update</button>
            </td>
        </tr>
        </tbody>
        @endforeach
    </table>
    {{ $designations->links() }}
  @endif

DesignationController.php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use App\Designation;

class DesignationController extends Controller
{
    public function index()
    {
       $designations = Designation::all();
       return view('pages.designation')->with('designations',$designations);
    }
}

You should use the paginate() method in your controller file.

public function index()
{
    $designations = Designation::paginate(10);
    return view('pages.designation')->with('designations',$designations); 
}

Paginating Eloquent Results

You may also paginate Eloquent queries. In this example, we will paginate the User model with 15 items per page. As you can see, the syntax is nearly identical to paginating query builder results:

$designations = Designation::paginate(15);

You may call paginate after setting other constraints on the query, such as where clauses:

$designations = Designation::where('column', 'value')->paginate(15);

You should use the paginate() method in your controller file.

public function index()
{
    $designations = Designation::paginate(15);
    return view('pages.designation')->compact('designations',$designations); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM