【问题标题】:Sort json object laravel排序 json 对象 laravel
【发布时间】:2018-07-08 02:59:13
【问题描述】:

我有两个合并的查询,我需要按 id 排序 (orderBy)。

$query1= DB::table('contract')->where('to_user_id',$to_user_id)->get();
$query2= DB::table('contract')->where('from_user_id',$to_user_id)->get();
$query1= $query1->merge($query2);

我尝试 orderBy('id') 但它不是一起订购的。

【问题讨论】:

标签: php json laravel


【解决方案1】:

与其执行 2 次查询并合并它们,不如只在 1 次中提取相关记录会更好吗?

例如:

$query1= DB::table('contract')
        ->where('to_user_id',$to_user_id)
        ->orWhere('from_user_id', $to_user_id)
        ->get();

【讨论】:

    【解决方案2】:

    您可以使用sortBy() 对集合进行排序,并使用values() 方法将键重置为consecutively 编号索引-

    $query1 = $query1->merge($query2);
    $query1 = $query1->sortBy('id');
    
    $query1 = $query1->values()->all();
    

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2011-09-03
      • 2017-06-07
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多