PHPackages                             sukohi/cahen - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. sukohi/cahen

ActiveLibrary[Database &amp; ORM](/categories/database)

sukohi/cahen
============

A PHP package mainly developed for Laravel to manage sort values of DB table automatically.

2.0.1(9y ago)012.9k1MITPHP

Since Aug 12Pushed 9y ago1 watchersCompare

[ Source](https://github.com/SUKOHI/Cahen)[ Packagist](https://packagist.org/packages/sukohi/cahen)[ RSS](/packages/sukohi-cahen/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (7)Used By (1)

Cahen
=====

[](#cahen)

A PHP package mainly developed for Laravel to manage sort values of DB table automatically.
(This is for Laravel 5+. [For Laravel 4.2](https://github.com/SUKOHI/Cahen/tree/1.0))

Installation
============

[](#installation)

Add this package name in composer.json

```
"require": {
  "sukohi/cahen": "2.*"
}

```

Execute composer command.

```
composer update

```

Register the service provider in app.php

```
'providers' => [
    ...Others...,
    Sukohi\Cahen\CahenServiceProvider::class,
]

```

Also alias

```
'aliases' => [
    ...Others...,
    'Cahen'   => Sukohi\Cahen\Facades\Cahen::class
]

```

Usage
=====

[](#usage)

**Basic**

```
$model = YourModel::find(1);
\Cahen::move($model)->to('your-column-name', 5);

```

**Up**

```
\Cahen::move($model)->up('your-column-name');

```

**Down**

```
\Cahen::move($model)->down('your-column-name');

```

**to First**

```
\Cahen::move($model)->first('your-column-name');

```

**to Last**

```
\Cahen::move($model)->last('your-column-name');

```

**with Transaction**

```
\DB::beginTransaction();

if(!\Cahen::move($model)->to('your-column-name', 5)) {

    \DB::rollback();

}

\DB::commit();

```

**with Where Clause**

You can use `where` clause to sort within specific record(s).

```
$model = YourModel::find(1);
\Cahen::move($model)
        ->where('column_1', '=', 'value')
        ->where('column_2', 'LIKE', '%value%')
        ->to('your-column-name', 5);

```

**Set data**

You can sort within specific record(s) by setting model object.

```
$moving_id = 1;
$model = YourModel::find($moving_id);
$models = YourModel::where('id', '
