2
votes

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);
    }
}
3
please add controller file code.Jinal Somaiya
@rika Add paginate() method in controller instaed of all().Amit Senjaliya

3 Answers

4
votes

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

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

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);
0
votes

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

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