Home> PHP Framework> Laravel> body text

How to implement custom style paging function in laravel

PHPz
Release: 2023-04-11 15:53:49
Original
698 people have browsed it

With the continuous development of the Laravel framework, more and more web projects are now choosing it. Of course, many developers also choose to use Laravel to develop their own websites. In daily development, for the implementation of functions, the usual approach is to first look for open source libraries or write components yourself. There are many previous experiences and summaries on the Internet. This article is one of them. It will tell you how to use it. Laravel comes with its own paging class to implement custom style paging.

Laravel’s own paging class

The Laravel framework comes with a paging library, which is very convenient to use. In the controller, we generally use thepaginate()method to query data and return pagination results, while in the Blade template we can directly use thelinks()method to render pagination links, see The following code:

// 在控制器中查询数据并返回分页结果 $data = DB::table('table_name')->paginate(15); // 在 Blade 模板中通过 links() 方法渲染分页链接 {{ $data->links() }}
Copy after login

In this way, the code completes the paging query and renders the paging link to the page. But this link style is the default. If we want to change the style, we need to customize the view.

Custom view

Let’s first understand thelinks()method. We can output it in the Blade template{{ $data->links () }}, the result is as follows:

Copy after login

We found that the paging link rendered by thelinks()method by default is an unordered list, each Each item is an independentlielement, where theactiveclass represents the current page number, and thedisabledclass represents the unavailable page number. If we want to customize the style of pagination links, we need to override Laravel's default link rendering in the view file.

In Laravel, you can use thephp artisan make:viewcommand to generate the view file, as follows:

php artisan make:view pagination
Copy after login

This command will be inresources/viewsCreate a file namedpagination.blade.phpin the directory. Write the following code in this file:

@if ($paginator->hasPages())  @endif
Copy after login

This code is Laravel’s default paging view code. We can copy it to thepagination.blade.phpfile and then Make custom modifications.

Custom style

The custom style depends on the developer's own preferences. For example, we can modify the paging link to a button style:

Copy after login

Since paging links usually There won’t be too many, and the paging link styles of different pages may also be different, so we only provide a simple modification method here, and developers can flexibly adjust it according to their own needs.

Summary

Through the introduction of this article, we have learned how the paging class that comes with the Laravel framework is implemented, and how to modify the style of paging links through custom views. Of course, this customization is not limited to pagination styles, developers can also apply it to various other layout styles.

In actual development, excellent developers can always discover the potential of the framework and optimize it according to their own needs. This is one of the technologies that must be mastered to become an excellent developer. I hope this article can be helpful to everyone, and I also hope that everyone can have a deeper understanding and application of the Laravel framework.

The above is the detailed content of How to implement custom style paging function in laravel. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!