PHP code example of hippieua / laravel-sortable
1. Go to this page and download the library: Download hippieua/laravel-sortable 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/ */
hippieua / laravel-sortable example snippets
$model->moveUp();
$model->moveDown();
$model->move('up');
$model->move('down');
Schema::table('comments', function (Blueprint $table) {
$table->unsignedInteger('order')->default(1);
});
use Hippie\Sortable\Sortable;
class Comment extends Model
{
use Sortable;
protected string $sortable_field = 'order';
protected $fillable = [
'order',
];
}
use Hippie\Sortable\Sortable;
class Comment extends Model
{
use Sortable;
protected string $sortable_field = 'order';
protected string $sortable_relation = 'post';
protected $fillable = [
'order',
];
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
class Post extends Model
{
public function comments(): BelongsTo
{
return $this->hasMany(Comment::class)->orderBy('order');
}
}
protected static function booted()
{
static::created(function ($model) {
$model->updateSortOrderOnCreate();
});
}
php
$comment = $post->comments()->create([
'text' => $request->text,
])
php
$post->comments()->first()->moveDown()
php
$comment->moveUp();
$comment->moveDown();
$comment->move('up');
$comment->move('down');