简体   繁体   English

如何基于第一个条件结果订购Laravel查询构建器

[英]How to order Laravel query builder based on first condition result

I have a company comparison web app that allows users to select 2 companies on different select drop-downs and compare them side by side. 我有一个公司比较网络应用程序,该应用程序允许用户在不同的选择下拉列表中选择2个公司,并进行比较。 Everything is working well so far except for the ordering. 到目前为止,除订购外,其他一切都运转良好。 I'd want the results of the comparison to be based on which item was queried first, rather than the default Laravel created_by field. 我希望比较的结果基于首先查询的项目,而不是默认的Laravel created_by字段。 Below is my code for the Query Builder: 以下是我用于查询生成器的代码:

public function compareCompanies(Request $request) {
    if ($request->has('companies') && $request->companies != null ) {
        $companies = array_filter($request->companies);

        $brands = Brand::with('categories')
        ->whereIn('brands.slug', $companies)
        ->get();
        return response()->json(['success' => true, 'brands' => $brands]);        
    }
}

And below is my ajax for displaying the response of the POST request 下面是我的ajax,用于显示POST请求的响应

$.ajax({
        type: "POST",
        url : url,
        data: $("#compareForm").serialize(),
        dataType : 'json',
        success : function(response) {

            if(response.success)
            {
                $("div#divLoading").removeClass('show');
                $.each(response.brands, function(i, brand){
                    var NewRow = '';
                    NewRow += '<table class="table"><thead><tr>';
                    NewRow += '<th>' + brand.brand + '</th>';
                    NewRow += '</tr></thead><tbody>';
                    NewRow += '<tr><td>'+brand.growth_score+'</td></tr>';
                    NewRow += '<tr><td>'+brand.mind_share +'</td></tr>';
                    NewRow += '<tr><td>'+brand.headquarters+'</td></tr>';
                    NewRow += '<tr><td>'+brand.revenue+'</td></tr>';
                    NewRow += '<tr><td>'+brand.no_of_employees+'</td></tr>';
                    NewRow += '</tbody></table></div>';
                    $("#companies").append(NewRow);
                })
            }
        },
        error: function (data) {
            console.log('An error occurred.');
            console.log(data);
        }
    });

The result I get is as shown on the image below: 我得到的结果如下图所示: 我得到的结果

As you can see, the result is swapped, since Laravel orders using the value of created_at. 如您所见,由于Laravel使用created_at的值进行排序,结果被交换了。 How can I order the result respective to the order of the input? 如何根据输入的顺序对结果进行排序?

Eloquent allows you to order the query results by specifying a column when using the orderBy method like so: Eloquent允许您在使用orderBy方法时通过指定列来对查询结果进行排序,如下所示:

 $brands = Brand::with('categories')
    ->whereIn('brands.slug', $companies)
    ->orderBy('column_name')
    ->get();

so if you submit the column name with your request, you can then dynamically change which column the data should be sorted by : 因此,如果您随请求一起提交列名,则可以动态更改数据应按以下方式排序的列:

 $brands = Brand::with('categories')
    ->whereIn('brands.slug', $companies)
    ->orderBy($request->column_name)
    ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM