以下是我的问题:
return App\AftermarketParts::where('alt', 'LIKE', "%$search%")
->orderBy('alt',"$direction")
->with('aftermarket_parts_type')
->get()
->groupBy('aftermarket_parts_type_id');
以下是我的数据集:
(16) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:alt:(...)
created_at:(...)
deleted_at:(...)
id:(...)
image:(...)
image_height:(...)
image_width:(...)
link:(...)
tires_and_rims_type:Object
created_at:(...)
deleted_at:(...)
description:(...)
id:(...)
name:(...)
updated_at:(...)
1:{…}
2:{…}
我正在尝试按名称对结果进行排序,当然这不起作用。
return App\AftermarketParts::where('alt', 'LIKE', "%$search%")
->orderBy('alt',"$direction")
->orderBy('name',"$name_direction")
->with('aftermarket_parts_type')
->get()
->groupBy('aftermarket_parts_type_id');
我也尝试过:
return DB::table('aftermarket_parts')
->join('aftermarket_parts_categories','aftermarket_parts_categories.id','=','aftermarket_parts.aftermarket_parts_type_id')
->select(DB::raw(
'aftermarket_parts.id,
aftermarket_parts.link,
aftermarket_parts.image,
aftermarket_parts.webpage_screen_shot,
aftermarket_parts.alt,
aftermarket_parts.image_width,
aftermarket_parts.image_height,
aftermarket_parts.aftermarket_parts_type_id,
aftermarket_parts.deleted_at,
aftermarket_parts.created_at,
aftermarket_parts.updated_at'))
->groupBy('name')
->orderBy('aftermarket_parts.alt', "$direction")
->orderBy('name', "$groupDirection")
->get();
返回DataSet:
data:Array(80)
0:
aftermarket_parts_type_id:(...)
alt:(...)
created_at:(...)
deleted_at:(...)
id:(...)
image:(...)
image_height:(...)
image_width:(...)
link:(...)
updated_at:(...)
webpage_screen_shot:(...)
即使它正确地返回了命令,它也会以我需要它的格式销毁对象数组。我想知道是否有办法通过雄辩的调用来完成这个或者我应该只使用php排序数组?想法?提前谢谢。
答案 0 :(得分:0)
我能想到的唯一解决方案是在不使用eloquent或sql的情况下反转数组顺序。它简单而且有效。
$AftermarketParts = App\AftermarketParts::where([
['alt', 'LIKE', "%$search%"]
])
->orderBy('alt',"$direction")
->with('aftermarket_parts_type')
->get()
->groupBy('aftermarket_parts_type_id');
$temp = [];
$j = 0;
for ($i=count($AftermarketParts); $i > 0; $i--) {
$temp[$j] = $AftermarketParts[$i];
$j++;
}
return $temp;
答案 1 :(得分:0)
最后一个解决方案颠倒了数组的顺序,这个数组使用冒泡排序对数组进行排序。
function bubbleSort($Array) {
$lastPos = count($Array);
for($lastPos -1; $lastPos >= 1; $lastPos--) {
for($i = 1; $i <= $lastPos -1; $i++) {
if($Array[$i][0]->aftermarket_parts_type->name > $Array[$i+1][0]->aftermarket_parts_type->name) {
$temp = $Array[$i];
$Array[$i] = $Array[$i+1];
$Array[$i+1] = $temp;
}
}
}
return $Array;
}