4

I've got 2 columns, Model and Action. First thing I would like to achieve is to order the model from A to Z.

I'm doing that with

orderBy('model', 'ASC')

Then I would like to order the action column on index, create, store etc. I've got a query, and i'me trying to sort the results in order: index, create, store, show, edit, update, destroy, [everything else].

However the result i'm getting is: [everything else], index, create, store, show, edit, update, destroy

Query:

Permission::orderBy('model', 'ASC')->orderByRaw("FIELD(action, 'index', 'create', 'store', 'show', 'edit', 'update', 'destroy')")->get();

Result should be something like:

  • model 1, index
  • model 1, create
  • model 1, store
  • etc
  • model 1, everything else
  • model 2, index
  • model 2, create
  • model 2, store
  • etc
  • model 2, everything else

Has anyone got an idea how I can fix this?

Thanks

4

1 回答 1

8

orderBy方法允许您按给定列对查询结果进行排序。orderBy 方法的第一个参数应该是您希望排序的列,而第二个参数控制排序的方向,可以是ascor desc,请查看示例:

$users = DB::table('table')
->orderBy('name', 'desc')  // You can pass as many columns as you want
->get();
于 2017-02-02T10:16:03.297 回答