Download the PHP package nevadskiy/laravel-position without Composer

On this page you can find all versions of the php package nevadskiy/laravel-position. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-position

Stand With Ukraine

🔢 Arrange Laravel models in a given order

PHPUnit Code Coverage Latest Stable Version License

✅ Requirements

🔌 Installation

Install the package via Composer:

`

🔨 Add positions to models

Add the HasPosition trait to the models that should have positions:

You also need to add a position column to the model's table using a migration:

And that's it!

📄 Documentation

How it works

Models with positions have an integer attribute named position, which indicates their position in the sequence. This attribute is automatically calculated upon insertion and is utilized for sorting the models during query operations.

Creating models

The position attribute is a kind of array index and is automatically inserted when a new model is created. For example:

Starting position

By default, the first record in the sequence is assigned a position value of 0. If you want to specify a custom number to start counting models, you can override the getStartPosition method in your model:

In this example, the first record will be assigned a position value of 1.

Ordering

By default, the newly created model is assigned the position at the end of the sequence.

For example, if you want to create models at the beginning of the sequence, you can override the getNextPosition method in your model:

In this example, each new model will be assigned the starting position and will be positioned at the beginning of the sequence. The positions of other models in the sequence will be automatically updated:

You can also use negative positions. For example, the -1 position indicates that the model will be positioned at the end of the sequence. It is almost identical to Model::count() - 1. This is the default behavior.

Deleting models

When a model is deleted, the positions of other records in the sequence are automatically updated.

Querying models

To select models in the arranged sequence, you can use the orderByPosition scope. For example:

Automatic ordering when querying models

The orderByPosition scope is not applied by default because the corresponding SQL statement will be added to all queries, even where it is not required.

It is recommended to manually add the scope in all places where you need it.

However, if you want to enable auto-ordering for all query operations, you can override the alwaysOrderByPosition method in your model as following:

Moving items

Update

To move a model to an arbitrary position in the sequence, you can simply update its position. For example:

The positions of other models will be automatically recalculated as well.

Move

You can also use the move method, which sets a new position value and updates the model immediately. For example:

If you want to move the model to the end of the sequence, you can use a negative position value. For example:

Swap

The swap method swaps the position of two models. For example:

Arrange models

It is also possible to arrange models by their IDs. The position of each model will be recalculated according to the index of its ID in the given array. You can also provide a second argument as a starting position. For example:

Grouping models

Position grouping is particularly useful when you want to maintain position sequences separately for different groups of records within the same table.

To enable position grouping, you can specify one or more database columns that act as the grouping criteria for positions using the groupPositionBy method in your model:

Position lock

By default, when you change the position or group of a model, the PositionObserver automatically updates the positions of other models in the sequence by performing additional database queries. If you need to disable this behavior for any reason, you can do it like so:

📑 Changelog

Please see CHANGELOG for more information what has changed recently.

☕ Contributing

Please see CONTRIBUTING for more information.

🔓 Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

📜 License

The MIT License (MIT). Please see LICENSE for more information.

🛠️ Todo List


All versions of laravel-position with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2|^8.0|^8.2
laravel/framework Version ^7.0|^8.0|^9.0|^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nevadskiy/laravel-position contains the following files

Loading the files please wait ....