合并2表并在laravel中使用数据表服务器端

时间:2018-07-24 06:45:42

标签: php laravel datatable serverside-javascript

我可以合并2个表并使用我的服务器端吗?

这是我的控制器,

public function getUser2(Request $request)
{

  $columns = array(
        0 => 'auto_num',
        1 => 'add_time',
  2 => 'id',
  3 => 'id_tes2'
    );

    $totalData = DataTes::count();
    $limit = $request->input('length');
    $start = $request->input('start');
    $order = $columns[$request->input('order.0.column')];
    $dir = $request->input('order.0.dir');

    if(empty($request->input('search.value'))){
        $posts = DataTes::offset($start)
                ->limit($limit)
                ->orderBy($order,$dir)
                ->get();
  $posts2 = DataTes2::offset($start)
                ->limit($limit)
                ->orderBy(DataTes::orderBy($order,$dir))
                ->get();
        $totalFiltered = DataTes::count();
    }else{
        $search = $request->input('search.value');
        $posts = DataTes::where('auto_num', 'like', "%{$search}%")
                        ->orWhere('add_time','like',"%{$search}%")
          ->orWhere('id','like',"%{$search}%")
                        ->offset($start)
                        ->limit($limit)
                        ->orderBy($order, $dir)
                        ->get();
  $posts2 = DataTes2::where('id_data_tes', 'like', $posts->id)
          ->where('data', 'like', "%{$search}%")
          ->offset($start)
                        ->limit($limit)
                        ->orderBy($order, $dir)
          ->get();
        $totalFiltered = DataTes::where('auto_num', 'like', "%{$search}%")
                        ->orWhere('add_time','like',"%{$search}%")
          ->orWhere('id','like',"%{$search}%")
                        ->count();
    }


    $data = array();
    if($posts && $posts2){
        foreach($posts as $r){
            foreach($posts2 as $r2){
      if ($r->id == $r2->id_data_tes){
        $nestedData['auto_num'] = $r->auto_num;
                $nestedData['add_time'] = $r->add_time;
        $nestedData['id'] = $r->id;
        $nestedData['id_tes2'] = $r2->data;
                $data[] = $nestedData;
      }
    }
        }
    }

    $json_data = array(
        "draw"          => intval($request->input('draw')),
        "recordsTotal"  => intval($totalData),
        "recordsFiltered" => intval($totalFiltered),
        "data"          => $data
    );

    echo json_encode($json_data);

}

我真的不知道那是否真的可能,我的代码完全错误,我只是尝试这样的代码,而且我真的不知道如何在Laravel 5.6中在1个视图中合并2个表并使用数据表服务器端

DataTes2是一个新表,我想合并到表DataTes

如果您知道的话,请帮助我,对不起我的英语不好,希望您理解我的意思。

0 个答案:

没有答案