Laravel Datatables 服务器处理输出

袁桐
2023-12-01

1.这是个简单的单表 Datatables  服务器处理,如果要做成插件就需要联表

public function post(Request $request)
    {
        $field = array(
            "tag",
            "title",
            "subtitle",
            "page_image",
            "meta_description",
            "reverse_direction"
        );
        $table = 'tags';
        $recordsTotal = 0;
        $recordsFiltered = 0;
        $start = ($request->input('start') == null)?0:$request->input('start');
        $length = ($request->input('length') == null)?10:$request->input('length');
        $column = $request->input('order')['0']['column'];
        $dir = $request->input('order')['0']['dir'];
        $query = DB::table($table);
        $recordsTotal = $query->count();
        if(strlen($request->input('search')['value']) > 0)
        {
            $keywords = $request->input('search')['value'];
            $where = implode('||',$field);
            $recordsFiltered = DB::select("select count(".$field[0].") from ".$table." where ".$where."  LIKE '%".$keywords."%'");
            foreach($field as $fs)
            {
                $query->orWhere($fs,'like','%'.$keywords.'%');
            }
        }else
        {
            $recordsFiltered = $recordsTotal;
        }
     
$data = $query->orderBy($field[$column],$dir)->offset($start)->limit($length)->get();
echo json_encode(array( "draw" => intval($request->input('draw')), "recordsTotal" => intval($recordsTotal), "recordsFiltered" => intval($recordsFiltered), "data" => $data ),JSON_UNESCAPED_UNICODE); }

 

转载于:https://www.cnblogs.com/yeyuansheng/p/7426329.html

 类似资料: