Skip to content

Instantly share code, notes, and snippets.

@huiralb
Created March 16, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huiralb/605c20df3995d368acff to your computer and use it in GitHub Desktop.
Save huiralb/605c20df3995d368acff to your computer and use it in GitHub Desktop.
Eloquent Order by Field
<?php
class CatalogController extends Controller
{
public function catalogLists()
{
$subcatids = implode(',', $this->subcategoryIds());
$subcategories = \App\Subcategory::whereIn('subcatid', $this->subcategoryIds())
->orderByRaw("FIELD(subcatid, $subcatids)")
->get();
// show subcategories name order by field
dd($subcategories->lists('name'));
}
/**
* Get Subcategory ids sort by submenus
* @return array subcategory id
*/
protected function subcategoryIds()
{
$subcatids = \App\Submenu::orderBy('id')->lists('subcategory_id');
$subcatids = array_where($subcatids, function($key, $subcatid){
return !is_null($subcatid);
});
return $subcatids;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment