Skip to content

Commit

Permalink
Optimize Code
Browse files Browse the repository at this point in the history
  • Loading branch information
s-damian committed Oct 13, 2024
1 parent 00ede12 commit 2743032
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 89 deletions.
88 changes: 49 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</a>
</p>

# Laravel Pagination for manual SELECT queries
# Laravel Pagination for Manual SELECT Queries

[![Tests](https://github.com/s-damian/laravel-man-pagination/actions/workflows/tests.yml/badge.svg)](https://github.com/s-damian/laravel-man-pagination/actions/workflows/tests.yml)
[![Static analysis](https://github.com/s-damian/laravel-man-pagination/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/s-damian/laravel-man-pagination/actions/workflows/static-analysis.yml)
Expand All @@ -14,7 +14,7 @@

## Laravel Manual Pagination - Laravel Man Pagination

### Introduction - Laravel Man Pagination package
### Introduction - Laravel Man Pagination

This package is particularly useful for implementing pagination with manual SELECT queries using `DB::select()`.

Expand All @@ -24,11 +24,21 @@ With this pagination, you will have no limit in a Laravel project to manage pagi

This pagination also allows you to generate a **per page**. This will generate a form HTML tag with a select HTML tag and clickable options.

> Paginate easily without limit 🚀
### Key Features

- Simple Laravel library for **pagination**.
- Customizable **per page** options.
- Support for multiple **languages**.
- Compatible with **Bootstrap 5**.

### Basic Example

> Paginate easily without limits 🚀
```php
<?php

$pagination = new Pagination();
$pagination = new SDamian\LaravelManPagination\Pagination();

$pagination->paginate($totalElements); // $totalElements: result of an SQL COUNT query

Expand All @@ -45,25 +55,26 @@ $offset = $pagination->offset();

### Author

This package is developed by [Stephen Damian](https://github.com/s-damian)
This package is developed by [Stephen Damian](https://github.com/s-damian).

> ✨ If you find this package useful, please **star it** on my GitHub repository
> ✨ If you find this package useful, please **star it** on the GitHub repository.
### Requirements

* PHP 8.0 || 8.1 || 8.2 || 8.3
* Laravel 8 || 9 || 10 || 11
- PHP 8.0 || 8.1 || 8.2 || 8.3
- Laravel 8 || 9 || 10 || 11


## Summary

* [Installation](#installation)
* [Customization with "vendor:publish"](#customization-with-vendorpublish)
* [Pagination instance methods](#pagination-instance-methods)
* [Examples](#examples)
* [Differences with Laravel integrated pagination](#differences-with-Laravel-integrated-pagination)
* [Support](#support)
* [License](#license)
- [Installation](#installation)
- [Customization with "vendor:publish"](#customization-with-vendorpublish)
- [Pagination instance methods](#pagination-instance-methods)
- [Examples](#examples)
- [Instance Options](#instance-options)
- [Differences with Laravel integrated pagination](#differences-with-Laravel-integrated-pagination)
- [Support](#support)
- [License](#license)


## Installation
Expand All @@ -75,7 +86,7 @@ composer require s-damian/laravel-man-pagination
```


## Customization with "vendor:publish"
## Customization With "vendor:publish"

### Custom Config and Lang and CSS

Expand All @@ -87,15 +98,13 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio

The `vendor:publish` command will generate these files:

* `config/man-pagination.php`

* `lang/vendor/man-pagination/{lang}/pagination.php`

* `public/vendor/man-pagination/css/pagination.css`
- `config/man-pagination.php`
- `lang/vendor/man-pagination/{lang}/pagination.php`
- `public/vendor/man-pagination/css/pagination.css`

You can of course customize these files.

### "vendor:publish" with "--tag" argument
### "vendor:publish" With "--tag" Argument

Publish only `config` file:

Expand Down Expand Up @@ -123,7 +132,7 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio
| void | __construct(array $options = []) | Constructor. |
| void | paginate(int $total) | (To use in the Controller) Activate the pagination. |
| null or int | limit() | (To use in the Controller) LIMIT: Number of items to retrieve. |
| null or int | offset() | (To use in the Controller) OFFSET: From where start the LIMIT. |
| null or int | offset() | (To use in the Controller) OFFSET: Starting point for the LIMIT. |
| int | total() | Determine the total number of matching items in the data store. |
| int | count() | Get the number of items for the current page. |
| int | firstItem() | Get the result number of the first item in the results. |
Expand All @@ -149,13 +158,15 @@ php artisan vendor:publish --provider="SDamian\LaravelManPagination\ManPaginatio

## Examples

### Concrete example with a manual SELECT query "DB::select"
### Concrete Example With a Manual SELECT Query "DB::select"

When doing "complex" SQL queries, sometimes you prefer to do it without Eloquent.

Here is an example of SQL query where this library is really useful:

```php
use SDamian\LaravelManPagination\Pagination;

$pagination = new Pagination();

$pagination->paginate($total);
Expand Down Expand Up @@ -220,7 +231,7 @@ $ordersAndInvoices = DB::select('

[![Laravel Man Pagination](https://raw.githubusercontent.com/s-damian/medias/main/packages/laravel-man-pagination-example.webp)](https://github.com/s-damian/larasort)

### Simple example with a manual SELECT query "DB::select"
### Simple Example With a Manual SELECT Query "DB::select"

Here is a simple example, following the MVC pattern, of how to use this library:

Expand Down Expand Up @@ -283,7 +294,7 @@ class CustomerController extends Controller
</div>
```

### Simple example of Controller with Eloquent
### Simple Example of Controller With Eloquent

This is for example only. Concretely, using Eloquent, you don't need this library. Because You can use Eloquent's **paginate** method.

Expand Down Expand Up @@ -316,11 +327,13 @@ class CustomerController extends Controller
```


## Add argument(s) to the instance
## Instance Options

```php
<?php

use SDamian\LaravelManPagination\Pagination;

// To change the number of elements per page:
$pagination = new Pagination(['pp' => 50]);
// Is 15 by default
Expand All @@ -344,37 +357,34 @@ $pagination = new Pagination(['per_page_name' => 'per_page']);
// The "per page" name is by default "pp".

// To change the CSS style of the pagination (to another CSS class as default):
$pagination = new Pagination(['css_class_p' => 'name-css-class-of-pagintion']);
$pagination = new Pagination(['css_class_p' => 'name-css-class-of-pagination']);
// The CSS class name is by default "pagination".

// To change the CSS style of the pagination active (to another CSS class as default):
$pagination = new Pagination(['css_class_link_active' => 'name-css-class-of-pagintion']);
$pagination = new Pagination(['css_class_link_active' => 'name-css-class-of-pagination']);
// The active CSS class name is by default "active".

// To change the CSS style of a per page (select) (to another id id as default):
// To change the CSS style of a per page (select) (to another id as default):
$pagination = new Pagination(['css_id_pp' => 'name-css-id-of-per-page-form']);
// The CSS ID name is by default "per-page-form".
// The CSS ID name is by default "per-page-form".
```


## Differences with Laravel integrated pagination
## Differences With Laravel Integrated Pagination

In this pagination, I tried to keep the conventions and the behavior of the pagination integrated in Laravel.

### I added extra "security":
### I Added Extra "Security":

If for example there are only 8 pages, and in the URL the visitor tries to go to page 9 (or to a page after page 9):

* The `onLastPage()` method will return `false` (whereas with the pagination integrated in Laravel, it returns true).

* The `currentPage()` method will return `1` (whereas with the pagination integrated in Laravel, it returns it returns the page in the URL).
- The `onLastPage()` method will return `false` (whereas with the pagination integrated in Laravel, it returns true).
- The `currentPage()` method will return `1` (whereas with the pagination integrated in Laravel, it returns it returns the page in the URL).


## Support

### Bugs and security Vulnerabilities

If you discover a bug or a security vulnerability, please send a message to Stephen. Thank you.
If you discover a **bug** or a **security vulnerability**, please send a message to Stephen. Thank you.

All bugs and all security vulnerabilities will be promptly addressed.

Expand Down
10 changes: 5 additions & 5 deletions src/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ protected function open(): string
{
$html = '';

$html .= '<nav>';
$html .= '<nav aria-label="Pagination">';
$html .= '<ul class="'.$this->pagination->getCssClassP().'">';

return $html;
}

/**
* If you are not on the 1st page, display: the left arrow (previous page).
* If you are not on the first page, display the left arrow (previous page).
*/
protected function previousLink(): string
{
Expand All @@ -45,7 +45,7 @@ protected function previousLink(): string
}

/**
* If you are not on the 1st page, make it appear: go to first page.
* If you are not on the first page, display a link to go to the first page.
*/
protected function firstLink(): string
{
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function paginationLink(int $nb): string
}

/**
* If you are not on the last page, display: go to last page.
* If you are not on the last page, display a link to go to the last page.
*/
protected function lastLink(): string
{
Expand All @@ -105,7 +105,7 @@ protected function lastLink(): string
}

/**
* If you are not on the last page, display: the right arrow (next page).
* If you are not on the last page, display the right arrow (next page).
*/
protected function nextLink(): string
{
Expand Down
Loading

0 comments on commit 2743032

Please sign in to comment.