Home > Enterprise >  Laravel Pagination To Certain Section
Laravel Pagination To Certain Section

Time:12-30

Guys I'm using Laravel's default pagination with bootstrap as its styling framework. I want to direct my page to certain html section after I click the pagination link, so far I only know how to paginate like the following codes:

Controller:

$data = Products::paginate(9);
return view('index', ['data' => $data]);

Blade:

{{ $data->links() }}

So, I want to direct my page to a certain section in html everytime I clicked the pagination link button. how can I achieve that?

I'm sorry for my poor explanation, hit me up if you need more detail.

Thank You

CodePudding user response:

you should make a blade file like this: pagination.blade.php

@if ($paginator->hasPages())
<ul >
    @if ($paginator->onFirstPage())
        <li >
            <a href="javascript:void(0)" >قبلی</a>
        </li>
    @else
        <li >
            <a href="javascript:void(0)"  data-page="{{ $paginator->currentPage()-1 }}">pre</a>
        </li>
    @endif
    @foreach ($elements as $element)
        @if (is_string($element))
            <li >
                <a href="javascript:void(0)"  >{{ $element }}</a>
            </li>
        @endif
        @if (is_array($element))
            @foreach ($element as $page => $url)
                @if ($page == $paginator->currentPage())
                    <li ><a href="javascript:void(0)" >{{ $page }}</a></li>
                @else
                    <li ><a href="javascript:void(0)"  data-page="{{ $page }}">{{ $page }}</a></li>
                @endif
            @endforeach
        @endif
    @endforeach
    {{-- next page  --}}
    @if ($paginator->hasMorePages())
        <li >
            <a href="javascript:void(0)"  data-page="{{ $paginator->currentPage() 1 }}">next</a>
        </li> 
    @else
        <li >
            <a href="javascript:void(0)" >بعدی</a>
        </li>
    @endif
</ul>
@endif

then create a blade file like this: table.blade.php

  @if(isset($permissions))
@php
    $paginator=$permissions;
@endphp
<table >
    <thead>
        <tr>
            heaers tags
        </tr>
    </thead>
    <tbody id="table-data">
        
        @foreach ($permissions as $permission)
            tr tags
        @endforeach
    </tbody>
</table>
@endif

<div >
    {{ $paginator->links('sections.pagination') }}
</div>

then in controller method:

public function index(Request $request)
{
    $permissions=Permission::paginate(5);
    if($request->ajax()){
        return view('sections.table',compact('permissions'));
    }
    return view('permission.index',compact('permissions'));
}
  • Related