php 如何在 Laravel 4 中对多列使用排序依据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17006309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:04:03  来源:igfitidea点击:

How to Use Order By for Multiple Columns in Laravel 4?

phplaravellaravel-4eloquentlaravel-query-builder

提问by Sophy

I want to sort multiple columns in Laravel 4 by using the method orderBy()in Laravel Eloquent. The query will be generated using Eloquent like this:

我想使用orderBy()Laravel Eloquent 中的方法对 Laravel 4 中的多列进行排序。查询将使用 Eloquent 生成,如下所示:

SELECT *
FROM mytable
ORDER BY
  coloumn1 DESC, coloumn2 ASC

How can I do this?

我怎样才能做到这一点?

回答by rmobis

Simply invoke orderBy()as many times as you need it. For instance:

只需orderBy()根据需要多次调用即可。例如:

User::orderBy('name', 'DESC')
    ->orderBy('email', 'ASC')
    ->get();

Produces the following query:

产生以下查询:

SELECT * FROM `users` ORDER BY `name` DESC, `email` ASC

回答by Sagar Naliyapara

You can do as @rmobis has specified in his answer, [Adding something more into it]

你可以按照@rmobis 在他的回答中指定的那样做,[添加更多内容]

Using order bytwice:

使用order by两次:

MyTable::orderBy('coloumn1', 'DESC')
    ->orderBy('coloumn2', 'ASC')
    ->get();

and the second way to do it is,

第二种方法是,

Using raw order by:

使用raw order by

MyTable::orderByRaw("coloumn1 DESC, coloumn2 ASC");
    ->get();

Both will produce same query as follow,

两者都会产生如下相同的查询,

SELECT * FROM `my_tables` ORDER BY `coloumn1` DESC, `coloumn2` ASC

As @rmobis specified in comment of first answer you can pass like an array to order by columnlike this,

正如@rmobis 在第一个答案的评论中指定的那样,您可以像这样传递一个数组以按列排序

$myTable->orders = array(
    array('column' => 'coloumn1', 'direction' => 'desc'), 
    array('column' => 'coloumn2', 'direction' => 'asc')
);

one more way to do it is iteratein loop,

另一种方法是iterate循环,

$query = DB::table('my_tables');

foreach ($request->get('order_by_columns') as $column => $direction) {
    $query->orderBy($column, $direction);
}

$results = $query->get();

Hope it helps :)

希望能帮助到你 :)

回答by mpemburn

Here's another dodge that I came up with for my base repository class where I needed to order by an arbitrary number of columns:

这是我为我的基本存储库类提出的另一个闪避,我需要按任意数量的列进行排序:

public function findAll(array $where = [], array $with = [], array $orderBy = [], int $limit = 10)
{
    $result = $this->model->with($with);
    $dataSet = $result->where($where)
        // Conditionally use $orderBy if not empty
        ->when(!empty($orderBy), function ($query) use ($orderBy) {
            // Break $orderBy into pairs
            $pairs = array_chunk($orderBy, 2);
            // Iterate over the pairs
            foreach ($pairs as $pair) {
                // Use the 'splat' to turn the pair into two arguments
                $query->orderBy(...$pair);
            }
        })
        ->paginate($limit)
        ->appends(Input::except('page'));

    return $dataSet;
}

Now, you can make your call like this:

现在,您可以这样拨打电话:

$allUsers = $userRepository->findAll([], [], ['name', 'DESC', 'email', 'ASC'], 100);