Skip to content

Instantly share code, notes, and snippets.

@fonclub
fonclub / 1-README.md
Created July 20, 2022 17:39 — forked from rcknr/1-README.md
GroupByPagination Trait - Laravel Eloquent Custom Pagination Example

GroupByPagination Trait

An example of custom pagination for Laravel Eloquent. In this scenario we have a model with a column (e.g. date) which we want to use to group records with for pagination but at the same time we still want to work with separate entities in the view.

The trait replaces QueryBuilder with a custom class which overrides forPage method used for pagination. It normally adds OFFSET and LIMIT parameters to the query which limit the number of results for a particular page. In our case though we are quering the datasource first for values of the defined groups and getting the minimum and maximum values to use them in the pagination query instead of LIMIT and OFFSET. We are also removing defined groups for the final query, so the pagination will use grouped dataset while results will be individual records.

After setting up the trait in a model you simply call paginate(). If no groups are defined the original forPage() method will be used instead.

Example:

@fonclub
fonclub / tokens.md
Created December 20, 2019 10:17 — forked from zmts/tokens.md
Про токены, JSON Web Tokens (JWT), аутентификацию и авторизацию. Token-Based Authentication

Про токены, JSON Web Tokens (JWT), аутентификацию и авторизацию. Token-Based Authentication

Last major update: 21.10.2019

Основы:

Аутентификация(authentication, от греч. αὐθεντικός [authentikos] – реальный, подлинный; от αὐθέντης [authentes] – автор) - это процесс проверки учётных данных пользователя (логин/пароль). Проверка подлинности пользователя путём сравнения введённого им логина/пароля с данными сохранёнными в базе данных.

Авторизация(authorization — разрешение, уполномочивание) - это проверка прав пользователя на доступ к определенным ресурсам.

Например после аутентификации юзер sasha получает право обращатся и получать от ресурса "super.com/vip" некие данные. Во время обращения юзера sasha к ресурсу vip система авторизации проверит имеет ли право юзер обращатся к этому ресурсу (проще говоря переходить по неким разрешенным ссылкам)