Eloquent 排序方式

Eloquent Order by

我有这样的代码来调用 eloquent 中的数据:

$args['orderby'] = array('id','asc');
$orderby  = (string) str_replace('"',"'", str_replace(']','',str_replace('[','',json_encode($args['orderby']))));
//Result $orderby = 'id','asc'

我将变量 orderby 放在方法 orderby eloquent 中,但这对我不起作用

Table::select($args['value'])->where($coloumn_search, 'ilike', '%'.$search.'%')->orderBy($orderby)->get();

如果我把 'id','asc' 放在方法 orderby 中 eloquent 这对我有用

Table::select($args['value'])->where($coloumn_search, 'ilike', '%'.$search.'%')->orderBy('id','asc')->get();

我创建的“id”、'asc'variabel orderby 有什么区别?

这是两个完全不同的东西:

  • ->orderBy($orderby): 一个数组参数
  • ->orderBy('id','asc'): 两个字符串参数

您可以使用这样的数组:

->orderBy(...$orderby)