如何使用多个参数创建 'yajra/laravel-datatables'?

How to create 'yajra/laravel-datatables' with multiple arguments?

如何从多个数据源创建 'yajra/laravel-datatables'?这里我想显示来自不同数据库的多个表。

 $data1  = DB::table('schema1.post AS p')
                        ->leftJoin('user AS u','u.id','=','p.user_id')
                        ->select('p.*','u.*')
                        ->get();   
 $data2  = DB::table('schema2.page AS page')
                        ->leftJoin('user AS u','u.id','=','page.user_id')
                        ->select('page.*','u.*')
                        ->get();   

  return Datatables::of($data)
            ->addIndexColumn()
            ->make(true);  

在这里我很困惑如何将2个变量传递给Datatables? 这是我的脚本。

var option = $("#Options").DataTable({
      processing: true,
      serverSide: false,
      ajax: "{{route('option')}}",
      columns: [
        {
          data: "user_name",
          name: "user_name",
        },
        {
          data: "type",
          name: "type",
        },
        {
          data: "description",
          name: "description",

        },
       });

您可以使用 Eloquent 的 ->union() 方法:

 $data1  = DB::table('schema1.post AS p')
                        ->leftJoin('user AS u','u.id','=','p.user_id')
                        ->select('p.*','u.*')
                        ->get();   
 $data2  = DB::table('schema2.page AS page')
                        ->leftJoin('user AS u','u.id','=','page.user_id')
                        ->select('page.*','u.*')
                        ->union($data1)
                        ->get();  

return Datatables::of($data2)
            ->addIndexColumn()
            ->make(true);  

确保您在 Datatables::of 方法中使用了正确的 $data2 变量。