If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Records के लिए Pagination काफी important feature है। Pagination के लिए Laravel paginate() method provide करता है ,जिसकी help से easily paginate कर पायंगे। paginate() method में simply एक page पर दिखाए जाने वाले page no. pass करना होता है , limit and offset automatically detect हो जाते हैं।
अगर आपको view page पर सिर्फ Next और Previous Link show करानी है , तो आप simplePaginate() method use कर सकते हैं।
माना आपको users की listing करनी है , वहां पर simple pagination कुछ इस तरह से use करेंगे।
File : app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
/**
* Show all of the users for the application.
*
* @return Response
*/
public function index()
{
$users = DB::table('users')->simplePaginate(15);
return view('user.index', compact('users'));
}
}
Example में page length 15 है , means users listing में एक बार में 15 records होंगे। अब अपने view page पर सिर्फ एक line लिखनी है।
<div class="container"> @foreach ($users as $user) {{ $user->name }} @endforeach </div> {!! $users->links() !!}
अब अगर आपको अपने page पर Next और Previous Link के अलावा page numbers भी show कराने हों तो उसके लिए आप paginate() method use कर सकते हैं। यह method Page Numbers भी show कराता है , जिससे आप उस particular page के records देख सकते हैं।
$users = DB::table('users')->simplePaginate(15);
/* हालाँकि आप Table Model भी use कर सकते हैं , जरूरी नहीं आप DB::table() ही use करें।*/
For Example :
$users = App\User::paginate(15);
appends() method का use करके आप current query string को pagination links के साथ easily append कर सकते हैं।
$users = App\User::paginate(15)->appends(request()->query()); /*in view file*/ {!! $users->appends(request()->query())->links() !!}
request()->query() current URL से query string data return करता है।
हालाँकि अगर आप Pagination Links में current query string की जगह custom query string append करना चाहते हैं तो append() method में request()->query() pass करने की वजाय [key => value] pair में Array pass कर दीजिये।
For Example
/*In your view file*/
{{ $users->appends(['sort' => 'votes'])->links() }}
/*It generates url like : www.your-domain.com/users?sort=votes */
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook