Created
March 16, 2016 01:55
-
-
Save huiralb/605c20df3995d368acff to your computer and use it in GitHub Desktop.
Eloquent Order by Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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