Part 20 - Pagination in Laravel [How to Build a Blog with Laravel 5 Series]

Поделиться
HTML-код
  • Опубликовано: 29 сен 2024
  • We have finished up our CRUD for our posts controller and now its time to add a few features before we move on into our next major resource for authentication.
    Today's feature is something you will find yourself doing a lot of when displaying large amounts of data. Pagination is the simple links at the bottom of the page that help us navigate large amounts of data.
    Pagination in the Laravel Framework is easy. We can call the simple paginate() method and laravel does the rest.
    We also learn how to order our results in descending order, to show the most recent items on top. This makes it easier to manage the posts we are working on now, which are the most recent posts.
    Coming Up:
    Pulling posts onto the homepage.
    -- CODE SNIPPETS --
    Pagination:
    Post::paginate(10);
    Order Results By:
    Post::orderBy('id', 'desc');
    -- DOWNLOAD SOURCE CODE --
    github.com/jac...
    -- LARAVEL DOCUMENTATION --
    laravel.com/do...
    -- MORE FROM THIS SERIES --
    Previous Video [Part 19]: • Part 9 - Migration Bas...
    Next video [Part 21]: • Part 21 - Query Builde...
    Full Playlist for the "How to Build a Blog with Laravel" Series: • How to Build a Blog wi...
    -- FOLLOW ME --
    Subscribe for New Releases!
    Twitter - / _jacurtis
    (ask me questions!)
    -- QUESTIONS? --
    Leave a comment below and I or someone else can help you.
    For quick questions you may also want to ask me on Twitter, I respond almost immediately.
    You may email me hello@jacurtis.com
    Thanks for all your support!
    ---
    Tags: Pagination to paginate in Laravel 5.2 Framework Tutorial Easy to learn. We also orderBy with Eloquent ORM and mysql database. Best tut about Laravel. Build a blog with Laravel Tutorial. Beginner tutorial. How to video. PHP framework easy best tutorial. Laravel 5 coding in PHP with Alex.

Комментарии • 99

  • @alibek1545
    @alibek1545 6 лет назад +6

    If you have problems with how bootstrap pagination rendered,
    following code might help you
    {{ $posts->links(pagination::bootstrap-4) }}

  • @nickita_dm
    @nickita_dm 8 лет назад

    Thank u for your lessons!!!

  • @thanosstantzouris7486
    @thanosstantzouris7486 4 года назад

    To populate the databases easily with Posts check out Laravel Factories! ;)
    Keep Coding!

  • @denyskvita349
    @denyskvita349 7 лет назад

    you are great man!!!!! thanks a lot

  • @haroonsateerrahimi5229
    @haroonsateerrahimi5229 6 лет назад

    Many Thanks

  • @JJ29621
    @JJ29621 6 лет назад

    Thanks Alex...Laravels pagination comes with Bootstrap 3, could you please explain how to use pagination with Bootstrap 4?

  • @sorin772
    @sorin772 6 лет назад

    where can i find all the available methods of this particular post object? TY

  • @rubelhossain_webdeveloper
    @rubelhossain_webdeveloper 8 лет назад

    Thanks

  • @GURURAJug
    @GURURAJug 8 лет назад

    Converting Results To JSON in pagination, please show one example on this and explain what is the use of this...thanks

  • @shahmirjadoon1322
    @shahmirjadoon1322 8 лет назад

    When I insert a new post it is saved in the database, but when i backSpace in Chrome, all my post is still available in the posts.create,
    so, how do I avoid it (I mean when I backspace, it returns me the posts page) ???

  • @parmarvishal21
    @parmarvishal21 6 лет назад

    sir do you have any video on search in laravel?

  • @preuton
    @preuton 6 лет назад

    I get only "next" and "previous" no numbers.

    • @preuton
      @preuton 6 лет назад

      Oops, i used simplePaginate() instead of paginate()

  • @babu-oh8ss
    @babu-oh8ss 8 лет назад

    Hi, how can we customize the pagination? for example if there are a lot of pages i would like to put 3 page links at first , 3 at last and dot dot dot in between, instead of showing all page links.

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад

      Yes you can, check out the documentation it explains it all. You will basically want to do some math with the helpers to display it manually.

  • @stiffstifflerful
    @stiffstifflerful 7 лет назад

    Hello, I am not using bootstrap. How can i customize the css for the pagination?

    • @stiffstifflerful
      @stiffstifflerful 7 лет назад

      ok, I did it, but I do not know if its the right way to do it. I created a new class called mypresenter and changed the html there and referred to that class in laravel/framework/src/illuminate/pagination/LengthAwarePaginator.php. It works but seems like a wrong way to do it.

    • @julyml
      @julyml 7 лет назад

      The function links() create a list with a class named "pagination" even if you're not use bootstrap, I think the easiest way to customize it, is just create your class in the stylesheet file. I've tested it just like it:
      .pagination {
      ~~~~whatever
      }

  • @7sujit12
    @7sujit12 8 лет назад

    Call to undefined method stdClass::links()

  • @babacardiallo5819
    @babacardiallo5819 5 лет назад

    you must stop speaking

  • @Osteele
    @Osteele 8 лет назад +5

    I added this to index.blade.php Page {{ $posts->currentPage() }} of {{ $posts->lastPage() }} I hope that you don't mind Alex. Not trying to "one up you" by any means. Just experimenting while I wait for your next video.

  • @TylarDurdenMMA
    @TylarDurdenMMA 8 лет назад +17

    You're the best! I'm following along to your videos on my work PC and when i get home i build it from memory which makes me retain the information a lot better. I cant wait until we get the user profile section. It would be really great if you can add a user profile picture option and a section to view/edit user account info. :) Thanks again!

    • @AhmedEssam-lv5vn
      @AhmedEssam-lv5vn 8 лет назад +1

      +John Darville hahhahaha actually um doing exactly the same! :D

  • @gromic-invest
    @gromic-invest 8 лет назад +9

    u can use $posts = Post::latest()->paginate(5);

    • @viery3654
      @viery3654 6 лет назад

      latest() orders by date, not id though.

  • @nitavshah8251
    @nitavshah8251 6 лет назад +1

    I am only getting the text for pagination like 1,2,3,4 links, but not the bar like Alex got. Laravel 5.5 can you help me

  • @nerdgames7649
    @nerdgames7649 8 лет назад +2

    To help with other Front-end Framework on pagination!
    @section('scripts')
    $( ".pagination" ).find( "li" ).addClass('waves-effect');
    @endsection
    This add a custom class to link on pagination.
    Thanks Jacurtis!

  • @manpreetkaur-xd4ft
    @manpreetkaur-xd4ft 6 лет назад

    How to Paginate following type of html response return to Ajax from controller in laravel
    foreach ($products as $key => $product) {
    $output.=”.
    ”.$product->id.”.
    ”.$product->title.”.
    ”.$product->description.”.
    ”.$product->price.”.
    ”;
    }
    return Response($output);

  • @ashokshrestha2780
    @ashokshrestha2780 7 лет назад +1

    why its showing Call to undefined method Illuminate\Database\Query\Builder::links()

  • @vaiz151
    @vaiz151 4 года назад

    can i ask u something sir, why while im using the links(), its doesnt show the style of pagination in the view like yours, im sorry if my grammar so bad, atleast u can understand me, its show without the styel, only a numbers and this symbol '< 123 >'

  • @adev_312
    @adev_312 7 лет назад +2

    If anyone is interested in seeding the database to make creating multiple posts fast and easy, I added some code below that should get you up and running.
    #Disclaimer: I'm running Laravel 5.4.13 so I'm not sure if this will work for older versions, I just thought it would be worth putting
    it out here incase anyone was interested.
    ###FIRST: create a PostTableSeeder.php file in database/seeds and add the code below...

    • @georgehoffman1762
      @georgehoffman1762 7 лет назад

      Great tips. I did that, too, but used fzaninotto/faker to create posts with lorem ipsum text, mainly because I'm lazy! Also, if you run the command "php artisan db:seed", artisan adds the new seeds to the existing data. It doesn't purge the existing data. At least in my version, which is 5.4.something. To purge and reseed in one step, you can run "php artisan migrate --seed", which rolls back, runs the migrations and then runs the master seeder.

  • @AbhimanyuNaikareWebDeveloper
    @AbhimanyuNaikareWebDeveloper 6 лет назад

    What do you mean by escaping the url's or links at 12:12? by {!!

  • @Mohit-zr7fd
    @Mohit-zr7fd 4 года назад

    How to custom query create LIKE
    pagination search form

  • @inakacidhe7417
    @inakacidhe7417 8 лет назад +4

    can u please make a viedo about restful api for beginners?? :( or just post some link where i can learn because it is so important :/

    • @JacurtisTutorials
      @JacurtisTutorials  8 лет назад +7

      +Ina Kacidhe yes i can. This is something ive had a lot of requests for.

    • @inakacidhe7417
      @inakacidhe7417 8 лет назад

      i hope you can do it asap...:) :) :)

  • @hamidelabassi5573
    @hamidelabassi5573 8 лет назад +3

    to show the pagination bar for laravel 5 you need to use $posts->render() instead of $posts->links()

    • @BoolFalse
      @BoolFalse 7 лет назад

      excuse me..as I understand, for >5.0 versions we need to use links() instead of render()
      is it means that ->render() and ->links() doing exactly same thing ??

    • @lilytekle2124
      @lilytekle2124 7 лет назад

      Hamid el Abassi thanks that was helpful

  • @AhmedEssam-lv5vn
    @AhmedEssam-lv5vn 8 лет назад +2

    instead of "SELECT `id` FROM `posts` ORDERED BY `id` DESC" its just "orderBy('id', 'desc')"! :D :D

  • @kamaboko1
    @kamaboko1 6 лет назад +1

    I created an 'old school' pagination solution using PHP a while ago, but this built-in feature is ridiculously easy.

  • @sylviamcclellan8842
    @sylviamcclellan8842 5 лет назад

    I cannot get pagination to work for me. I am using Laravel 5.7 and it is coming up with error "call to undefined method" with both render() and links()... Please Please help!

    • @uristmsk
      @uristmsk 5 лет назад

      try {!! $posts->render() !!} or {!! $posts->links() !!} and use $posts - not $post , I made this mistake first)

  • @asiffoysal1848
    @asiffoysal1848 4 года назад

    sir i have a problem in search blade pagination...

  • @singowl2833
    @singowl2833 5 лет назад

    how to customize the pagination style?

  • @amirkamalian8045
    @amirkamalian8045 6 лет назад

    Alex YOU ARE THE BEST YOU ARE THE BEST

  • @dancostinel5829
    @dancostinel5829 8 лет назад

    I created the following improvements: 1. I used nl2br filter to let the user insert blank lines; 2. I used bootstrap glyphicons for every button to make them look as nicer as they can be, as well as I used he "title" attribute for each of them. Question: Why you created a new route for "Read More" button, when you already have the posts.view (or posts.show)? Why not linking directly to that page?

  • @SavilliMillard
    @SavilliMillard 7 лет назад

    Great tut!
    I've a problem with the page=N.
    No matter what, I will only get the first set of objects that I retrieve.
    E.g.
    →paginate(5)
    ?page=1 and ?page=4
    gets the exact some results...

  • @michaelangelo4730
    @michaelangelo4730 8 лет назад

    Hi, I added DELETE action button on index page using the same working DELETE code from another part of this tutorial series and i got an error saying undefined variable post. Why is that same DELETE code didn't work on index page? Is that because the "paginate(#)" from PostController?

  • @tulgaerarslan1693
    @tulgaerarslan1693 6 лет назад

    Did anybody render the pagination with Semantic UI. I am also learning how to use Seamantic. So far I was able to complete what Alex teaches by using Semantic UI instead. However, at this point I am stuck. Laravel doesn't recognize my semantic formatting. As far as I understand, pagination feature in Laravel defaults to Bootsrap. I was't able force Laravel to use Semantic UI instead. If anybody has experience with this, I would greatly appreciate your input. This is a great course BTW, I've learned tons already. Thank you Alex.

  • @TheMakita1985
    @TheMakita1985 8 лет назад

    I am shocked how easy pagination with Laravel.I was doing this just with php,bootstrap and jqyery....it's not simply at all...Lavarel is awesome! Once again,than you Alex!
    Greetings from Serbia

  • @shqawe
    @shqawe 8 лет назад

    Hi Alex, i have one problem when i use pagination with two tables i got just the first 5 records but if select more than 5 records i got error but when i use pagination with one table it's work perfectly this is my controller code:
    $data = Posts::with('category')->paginate(5);
    return view('temps.posts')->with('data', $data);

  • @_rachid
    @_rachid 8 лет назад +1

    Indeed! Laravel is powerful!

  • @MuhammadAdnan-gx6rd
    @MuhammadAdnan-gx6rd 5 лет назад

    And now he's dead maybe ?

  • @devaiaizarry2235
    @devaiaizarry2235 8 лет назад

    +Jacurtis i don't know if this is important but i was thinking about creating a sort option, like a dropdown with Newest/Oldest or sort by alphabetical order is this possible is it? cuz i want to make it soon if i've master this hehe im new that's why .. btw Thanks for the tutorials More power.

  • @Kliamframe
    @Kliamframe 7 лет назад

    This took me so loooong time, to figure out with pure/raw PHP. In Laravel not even 20 minutes...

  • @sebastianfarias5670
    @sebastianfarias5670 7 лет назад

    Just want to let you know, I know there's a more updated series on your channel on how to build a blog but I really want to hear everything you have to say in every video you post!!!!! This is by far the best series I have ever followed (NETFLIX included) hahahaha

  • @sumitdhanania7589
    @sumitdhanania7589 7 лет назад

    Hey Alex, first of all i would thank u a lot for making such an awesome series on laravel. You made it super easy to understand the basics of what laravel is. and em pretty excited to look whats interesting coming in the upcoming videos.
    Secondly i would request you to please make a tutorial on how to use ajax with laravel. I want to learn that concept and the way you explain it , would be an bonus for many. So I hope you will look after my request and thanks again. cheers !!

  • @1lucak1
    @1lucak1 7 лет назад

    Is there a video for showing just like 10 items and then displaying a "show more" button? It's kind of going into the same driection as pagination...

  • @kaiser741
    @kaiser741 7 лет назад

    btw Alex, what is ->get( ) for anyway? why you didn't use it here in the postController, but you use it in the pagesController in the next video?

  • @hichamabboud9672
    @hichamabboud9672 4 года назад

    man you rock!!

  • @minjon56
    @minjon56 8 лет назад

    Laravel pagination is absolutely awesome. And Alex, you are the best! Your explanations are so clear that I, who knew nothing about Laravel before starting following your tutorials, managed to insert serial numbers into the first col of the table instead of posts ids. To me it makes more sense b/c, some ids are missing after deleting a post.

  • @ariefbudiprasetyo1440
    @ariefbudiprasetyo1440 7 лет назад

    Hi, can you make tutorial how to remove ?page= from pagination url ?
    i have problem with this, please help

  • @alexmunyua4781
    @alexmunyua4781 8 лет назад

    great stuff

  • @aleeawann
    @aleeawann 8 лет назад

    hey curtis, Can't find the link to that video you discussed about in the beginning.

  • @alirezasaharkhiz268
    @alirezasaharkhiz268 8 лет назад

    i worked with laravel 4 and i try to get some coding update for 5.2 i watched yor vids and they were very good tnx ;)

  • @petarbaykov9621
    @petarbaykov9621 8 лет назад

    Than you man! It's very helping for me to learn Laravel basics.

  • @maheshbudhathoki1720
    @maheshbudhathoki1720 8 лет назад

    Jacurtis(Alex) cool n perfect instructor for laravel learner like me .... excellent job bro

  • @rajivkumargupta8467
    @rajivkumargupta8467 7 лет назад

    pagination is not working in laravel 5.5 while search record from ajax

  • @officialtiktokmatches
    @officialtiktokmatches 7 лет назад

    no words. You are such a great man ! all the videos are very very helpful. Salute you sir !

  • @yavuzkoca8352
    @yavuzkoca8352 7 лет назад

    Dude, it cannot be soo much easy like that :D Laravel

  • @MathanaCherput
    @MathanaCherput 7 лет назад

    man your videos really helped me to learn new stuff

  • @ShampooWow
    @ShampooWow 8 лет назад

    Great video! I like it

  • @ablaze2763
    @ablaze2763 8 лет назад

    I'm just curious, what do you record your screen with?

  • @irwinpalomo7212
    @irwinpalomo7212 7 лет назад

    and how do you create a paginate but using the union method.

  • @karimkhan4205
    @karimkhan4205 8 лет назад

    hello it is awesome tutorial for me . thank you very much

  • @mikemcgrath579
    @mikemcgrath579 7 лет назад

    amazing series. better than 99% of the paid sites.

  • @luispasten2494
    @luispasten2494 7 лет назад

    Explicas muy muy bien !! xd saludos desde México

  • @codkylla3281
    @codkylla3281 8 лет назад

    I have been following along. I had some problems with the session flash. But i have resolved. just wanna thank you. I love the way your tutorials are . soooooooo awesome. Looking forward to more from you. Expecially android apps.

  • @learnwithharry8971
    @learnwithharry8971 7 лет назад

    awesome, thank you you made my day easy :-)

  • @amrgaber4662
    @amrgaber4662 8 лет назад

    thanks alex again
    tutorial is so easy

  • @moemengaballa
    @moemengaballa 6 лет назад

    Thanks Alex

  • @berryblueblossom3503
    @berryblueblossom3503 7 лет назад

    is links() default ?

  • @candybar4761
    @candybar4761 7 лет назад

    Till this video is series there is a slight problem with titles column. If you try to add 255 characters in a title -- the column will be huge.

    • @JacurtisTutorials
      @JacurtisTutorials  7 лет назад

      You can add a max-width in your css if this is a major concern. Then the title would wrap. Maybe do a max width of 25% and it would maintain it.

    • @candybar4761
      @candybar4761 7 лет назад

      Alex, thanks! It was not a major concern, I just wanted to highlight this in order to make things clean for people following this series.

  • @WhatDoYouMean002
    @WhatDoYouMean002 6 лет назад

    The beginning : 3:16

  • @k.mukhami
    @k.mukhami 5 лет назад

    THANK YOU!!!

  • @kenxu5721
    @kenxu5721 7 лет назад

    awesome!

  • @longdau199
    @longdau199 6 лет назад

    thanks