在仍然通过关系进行访问时,是否可以使用单独的ID数组来订购关系集合?
The setup is Checklist
has many ChecklistItem
s and the desired order of the related items exists as a property Checklist::$item_order
. It is just an array of numeric ID's in the user's desired order.
:
class Checklist extends Model {
protected $casts = ['item_order' => 'array'];
public function items() {
return $this->hasMany(ChecklistItem::class);
}
}
class ChecklistItem extends Model {
public function list() {
return $this->belongsTo(Checklist::class);
}
}
I access this relationship the normal way:
$list = Checklist::find(1234);
foreach ($list->items as $item) {
// More Code Here
}
Is there a feasible way to order $list->items
based on the values in the $list->item_order
array?
(我不能只在"项目"表中添加"订单"列,b/c每个"列表"的订单都会更改.)