PHP code example of flyhjaelp / laravel-eloquent-orderable
1. Go to this page and download the library: Download flyhjaelp/laravel-eloquent-orderable 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/ */
flyhjaelp / laravel-eloquent-orderable example snippets
Schema::create('orderable_test_models', function (Blueprint $table) {
$table->unsignedInteger('order')->nullable();
});
use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\Orderable;
use Illuminate\Database\Eloquent\Model;
class Foobar extends Model implements OrderableInterface { //implement the orderable interface
use Orderable; //use the orderable trait
}
$foobarA = (new Foobar())->save();
$foobarB = (new Foobar())->save();
$foobarC = (new Foobar())->save();
Foobar::all()->pluck('order','id');
// will output [1 => 1, 2 => 2, 3 => 3]
use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\OrderableWithinGroup;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class MenuItem extends Model implements OrderableInterface { //implement the orderable interface
use OrderableWithinGroup; //use the orderableWithinGroup trait
public function scopeOrdered(Builder $query): void{
$query->orderBy('menu_id')->orderBy('order');
}
public function scopeWithinOrderGroup(Builder $query, OrderableInterface $orderableModel): void{
$query->where('menu_id',$orderableModel->menu_id);
}
public function columnsAffectingOrderGroup(): Collection{
return collect(['menu_id']);
}
}
$foobarA = new Foobar();
$foobarA->menu_id = 1;
$foobarA->save();
$foobarB= new Foobar();
$foobarB->menu_id = 1;
$foobarB->save();
$foobarC = new Foobar();
$foobarC->menu_id = 2;
$foobarC->save();
Foobar::all()->pluck('order','id');
// will output [1 => 1, 2 => 2, 3 => 1]
use Flyhjaelp\LaravelEloquentOrderable\Interfaces\OrderableInterface;
use Flyhjaelp\LaravelEloquentOrderable\Traits\PivotOrderable;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Builder;
class JourneyCheckpointsRelationship extends Pivot implements OrderableInterface{
use PivotOrderable;
public $incrementing = true;
public function scopeWithinOrderGroup($query, OrderableInterface $orderableModel)
{
return $query->where('journey_id', $orderableModel->journey_id);
}
public function scopeOrdered(Builder $query): void
{
$query->orderBy('journey_id')->orderBy($this->getOrderableColumn());
}
public function columnsAffectingOrderGroup(): Collection
{
return collect(['journey_id']);
}
}
use Illuminate\Database\Eloquent\Model;
class Journey extends Model{
public function checkpoints() {
return $this
->belongsToMany(Checkpoint::class)
->using(JourneyCheckpointsRelationship::class)
->withPivot('order')
->orderBy('pivot_order');
}
}
public function getOrderableColumn(): string {
return 'non_default_order_column';
}
public function scopeOrdered(Builder $query): void{
$query->orderBy('menu_id')->orderBy('order');
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.