PHP code example of arneetsingh / laravel-customsort

1. Go to this page and download the library: Download arneetsingh/laravel-customsort library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arneetsingh / laravel-customsort example snippets




namespace App\Models;

use ArneetSingh\CustomSort\Traits\CanCustomSort;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use CanCustomSort;
}


Post::setNewOrder([2, 4, 1, 5, 3]);

Post::orderByCustom()->get();

$post->setOrderPriority(10);

Request: PUT
Endpoint: /posts/customSort
Payload:
{
	"custom_sort":[
		{
			"id":2,
			"priority":5			
		},
		{
			"id":4,
			"priority":4			
		},
		{
			"id":1,
			"priority":3			
		},
		{
			"id":5,
			"priority":2			
		},
		{
			"id":3,
			"priority":1			
		}
	]
}


    public function store(Request $request)
    {
        $request->validate([
            'custom_sort' => 'ed|integer'
        ]);
        
        $morphClass = (new Post())->customSort()->getMorphClass();
        collect($request->custom_sort)->transform(function ($item) use($morphClass) {
            CustomSort::create([
                'sortable_id' => $item['id'],
                'sortable_type' => $morphClass,
                'priority' => $item['priority']
            ]);
        });
    }

bash
php artisan vendor:publish --tag="laravel-customsort-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-customsort-config"