TableBuilder: custom sort
Created 7 years ago by pepijn

in a TableBuilder, is there a way to define an OrderHandler / SortHandler where I am free to manipulate the entries collection to the order I want ?

ryanthompson  —  7 years ago

Set the sortable option to true in protected $options = []; and you can manually order them.

pepijn  —  7 years ago

@ryanthompson Thanks for your input! However, this is still it's unclear to me. I really want to implement a custom sort on the collection, using something like below. Where exactly could I do this in a TableBuilder?

// first remove the entry that I always want on top
$models = $models->filter(function($item) {
   return $item->id != 1;
});

// now sort them alphabetically
$models->sortBy(function($item) {
return $item->title;
});

// finally, prepend the entry I always want on top
$stickyModel = MyModel::where('id', 1)->first();
$models->prepend($stickyModel);