1. Go to this page and download the library: Download baril/orderly 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/ */
public function up()
{
Schema::create('articles', function (Blueprint $table) {
// ... other fields ...
$table->unsignedInteger('position');
});
}
class Article extends Model
{
use \Baril\Orderly\Concerns\Orderable;
protected $guarded = ['position'];
}
class Article extends Model
{
use \Baril\Orderly\Concerns\Orderable;
protected $orderColumn = 'order';
protected $guarded = ['order'];
}
$model = Article::find(1);
$anotherModel = Article::find(10)
$model->moveAfter($anotherModel);
// $model is now positioned after $anotherModel, and both have been saved
$entity = Article::find(10);
$entity->next(10); // returns a QueryBuilder on the next 10 entities, ordered
$entity->previous(5)->get(); // returns a collection with the previous 5 entities, in reverse order
$entity->next()->first(); // returns the next entity
$collection = Article::orderBy('publication_date', 'desc')->get();
// $collection is not a regular Eloquent collection object, it's a custom class
// with the following additional method:
$collection->saveOrder();
$ordered = $collection->setOrder([4, 5, 2]);
// This will reorder all statuses (assuming there are 5 statuses in the table):
Status::setOrder([2, 1, 5, 3, 4]);
// This will put the status with id 4 at the beginning, and move the other
// statuses' positions accordingly:
Status::setOrder([4]);
// This will only swap the statuses 3, 4 and 5, and won't change the position
// of the other statuses:
Status::whereKey([3, 4, 5])->setOrder([4, 5, 3]);
class Article extends Model
{
use \Baril\Orderly\Concerns\Orderable;
protected $guarded = ['position'];
protected $groupColumn = 'section_id';
}
class Section extends Model
{
public function articles()
{
return $this->hasMany(Article::class)->ordered();
// Chaining the ->ordered() method is optional here, but you can do
// it if you want the relation ordered by default.
}
}
class Article extends Model
{
protected $groupColumn = 'section_id';
}
class Post extends Model
{
use \Baril\Orderly\Concerns\HasOrderableRelationships;
public function tags()
{
return $this->belongsToManyOrderable(Tag::class);
}
}
$post->tags()->attach($tag->id); // will attach $tag and give it the last position
$post->tags()->sync([$tag1->id, $tag2->id, $tag3->id]) // will keep the provided order
$post->tags()->detach($tag->id); // will decrement the position of subsequent $tags
class Post extends Model
{
use \Baril\Orderly\Concerns\HasOrderableRelationships;
public function tags()
{
return $this->belongsToManyOrdered(Tag::class);
// the line above is actually just a shortcut to:
// return $this->belongsToManyOrderable(Tag::class)->ordered();
}
}
$post->tags; // ordered by position, because of the definition above
$post->tags()->ordered('desc')->get(); // reverse order
$post->tags()->unordered()->get(); // unordered
// Note that orderBy has no effect here since the tags are already ordered by position:
$post->tags()->orderBy('id')->get();
// This is the proper way to do it:
$post->tags()->unordered()->orderBy('id')->get();
$tag1 = $article->tags()->ordered()->first();
$tag2 = $article->tags()->ordered()->last();
$article->tags()->moveBefore($tag1, $tag2);
// now $tag1 is at the second to last position
$article->tags()->setOrder([$id1, $id2, $id3]);
class Post extends Model
{
use \Baril\Orderly\Concerns\HasOrderableRelationships;
public function tags()
{
return $this->morphToManyOrderable('App\Tag', 'taggable', 'tag_order');
}
}
class Tag extends Model
{
use \Baril\Orderable\Concerns\HasOrderableRelationships;
public function posts()
{
return $this->morphedByManyOrderable('App\Post', 'taggable', 'order');
}
public function videos()
{
return $this->morphedByManyOrderable('App\Video', 'taggable', 'order');
}
}