<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rethinking / eloquent-sortable-relations example snippets
use Illuminate\Database\Eloquent\Model;
use Rethinking\Eloquent\Relations\Sortable\HasSortedRelations;
class Owner extends Model
{
use HasSortedRelations;
public function items()
{
return $this->hasManySorted(RelatedItem::class);
}
}
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Rethinking\Eloquent\Relations\Sortable\HasSortingContext;
use Rethinking\Eloquent\Relations\Sortable\Sortable;
class RelatedItem extends Model implements Sortable
{
use HasSortingContext;
public function owner(): BelongsTo
{
return $this->belongsTo(Owner::class);
}
public function getSortingContext(): BelongsTo
{
return $this->owner();
}
}
public static function getPositionColumnName(): string
{
return 'position';
}
//assume $owner has 3 related items with id 1,2,3 and positions of 1,2,3
$owner = Owner::find(1);
$owner->items()->setSortingOrder([2,3,1]);
$ids = $owner->items->pluck('id')->toArray(); //ids = [2,3,1]
//assume $owner has 3 related items with corresponding positions of [1,3,5]
$owner = Owner::find(1);
$owner->items()->resort();
$positions = $owner->items->pluck('position')->toArray(); //$positions = [1,2,3]
$owner = Owner::find(1);
$owner->items()->create(); //newly created item will have position = 1
$owner->items()->save(new Item()); //newly created item will have position = 2
$item = new Item();
$item->owner()->associate($owner);
$item->save(); //newly created item will have position = 3
$item = Item::find(2);
//assume $item has position = 2
$item->owner()->dissociate();
assert($item->position === null); //true
//assume $owner has 3 related items with id 1,2,3 and positions of 3,1,2
$owner = Owner::find(1);
$items = $owner->items()->get(); // items will be sorted by position i.e. ids will be [$item2,$item1,$item3]
//assume you have $owner -> $middle -> $item, where $item is sorted by $middle context
use Illuminate\Database\Eloquent\Model;
use Rethinking\Eloquent\Relations\Sortable\HasSortedRelations;
use Rethinking\Eloquent\Relations\Sortable\HasSortingContext;use Rethinking\Eloquent\Relations\Sortable\Sortable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Owner extends Model
{
use HasSortedRelations;
public function middles()
{
return $this->hasMany(Middle::class);
}
public function items()
{
return $this->hasManySortedThrough(Item::class, Middle::class);
}
}
class Middle extends Model
{
use HasSortedRelations;
public function items()
{
return $this->hasManySorted(Item::class);
}
public function owner()
{
return $this->belongsTo(Owner::class);
}
}
class Item extends Model implements Sortable
{
use HasSortingContext;
public function middle()
{
return $this->belongsTo(Middle::class);
}
public function getSortingContext(): BelongsTo
{
return $this->middle();
}
}
//assume $owner has two $middle, each $middle has 3 $item
//items inside every $middle has sorting of [1,2,3]
$owner = Owner::find(1);
//items will be mixed i.e. $middle1Item1, $middle2Item2, $middle1Item2, $middle2Item2 etc.
$items = $owner->items()->get();
//assume you have $owner -> $middle -> $item, where $middle is sorted by $owner context
use Illuminate\Database\Eloquent\Model;
use Rethinking\Eloquent\Relations\Sortable\HasSortedRelations;
use Rethinking\Eloquent\Relations\Sortable\HasSortingContext;use Rethinking\Eloquent\Relations\Sortable\Sortable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Owner extends Model
{
use HasSortedRelations;
public function middles()
{
return $this->hasMany(Middle::class);
}
public function items()
{
return $this->hasManySortedThrough(Item::class, Middle::class);
}
}
class Middle extends Model implements Sortable
{
use HasSortingContext;
public function items()
{
return $this->hasMany(Item::class);
}
public function owner()
{
return $this->belongsTo(Owner::class);
}
public function getSortingContext() : BelongsTo
{
return $this->owner();
}
}
class Item extends Model
{
public function middle()
{
return $this->belongsTo(Middle::class);
}
}
//assume $owner has two $middle, each $middle has 3 $item
//$middle1 has position 2
//$middle2 has position 1
$owner = Owner::find(1);
//items will be fetched from in order of $middle2 (position 1) and $middle1 (position 2)
$items = $owner->items()->get();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.