Sorting Laravel Collection via Array of ID’s

You can do this: $order = $list->item_order; $list->items->sortBy(function($model) use ($order){ return array_search($model->getKey(), $order); } Also you could add an attribute accessor to your model which does the same public function getSortedItemsAttribute() { if ( ! is_null($this->item_order)) { $order = $this->item_order; $list = $this->items->sortBy(function($model) use ($order){ return array_search($model->getKey(), $order); }); return $list; } return $this->items; } … Read more

Automatically deleting related rows in Laravel (Eloquent ORM)

I believe this is a perfect use-case for Eloquent events (http://laravel.com/docs/eloquent#model-events). You can use the “deleting” event to do the cleanup: class User extends Eloquent { public function photos() { return $this->has_many(‘Photo’); } // this is a recommended way to declare event handlers public static function boot() { parent::boot(); static::deleting(function($user) { // before delete() method … Read more