Laravel中使用JSON对象进行分页

时间:2018-10-17 07:57:52

标签: laravel datatables client-side

您好,我有xml格式的数据,并且我尝试用JSON进行转换。我想在拉拉维尔做些讨价还价。在这里,我在控制器中给出我的代码。而且我的代码出现错误

错误:

  Pagep.php第189行中的

FatalErrorException:调用成员   在字符串上使用paginate()函数

这是我的控制器:

public function soap(Request $request){ 
            $this->validate($request, [
            'nama' => 'required',
            'negara' => 'required',
            'pencarian'=>'required'
            ]);

            $count = Pengaturan::where('key', 'visitor')        
            ->first();
            $array=array_map('intval', str_split($count->value));

            $kategori = $request->input("pencarian");
            $nama = $request->input("nama");
            $negara = $request->input("negara");
            $pages = $request->get("pageby");

            $soapClient = new SoapClient("http://interop.bnp2tki.go.id/perwakilanWS/wsdl"); 
            $param;     
            if($kategori=="2"){ //search by paspor
                $param = array( 
                'user_id'=>'kjri_jed', 
                'user_pass'=>'123',
                'paspor'=>$nama         
                );

                $error = 0; 
                try { 
                    $info = $soapClient->__call("ws_view_tkibypaspor", $param);         
                    if($info!="notfound"){                  
                        $informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
                        $xml = new SimpleXMLElement($informasi);
                        $data = $xml->tki_data;
                        $pagess = $xml->total_page;
                        $hasil = array($data);
                        $json = json_encode($hasil);
                        $paging = $json->paginate(10);

                        return view('page.search-tki', compact('data', 'array','paging'));
                        }else{
                        $data=$info;
                        return view('page.search-tki', compact('data', 'array'));                   
                    }
                    } catch (SoapFault $fault) { 
                    return view('page.search-tki', compact('data', 'array'));
                } 

                }else{ //by nama
                $param = array( 
                'user_id'=>'kjri_jed', 
                'user_pass'=>'123',
                'nama'=>$nama,
                'negara'=>$negara,
                'page'=>$pages          
                );
                $error = 0; 

                try { 
                    $info = $soapClient->__call("ws_view_tkibyname", $param); 
                    if($info!="notfound"){
                        $informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
                        $xml = new SimpleXMLElement($informasi);
                        $data = $xml->tki_data;
                        $pagess = $xml->total_page;

                        //merubah ke pagination
                        //$hasil = array($data);
                        $json = json_encode($data);
                        $paging = $json->paginate(10);

                        return view('page.search-tki', compact('data', 'array','pagess','nama','negara','kategori','json','paging'));
                        }else{
                        $data=$info;
                        return view('page.search-tki', compact('data', 'array'));                   
                    }
                    } catch (SoapFault $fault) { 
                    return view('page.search-tki', compact('data', 'array'));
                } 

            }       
        }

有人可以帮助我吗? 请:)

1 个答案:

答案 0 :(得分:0)

方法paginate()适用于QueryBuilder::class实例。

您可以使用laravel Collection

$informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
$xml = new SimpleXMLElement($informasi);
$data = $xml->tki_data;
$pagess = $xml->total_page;
$hasil = array($data);
$page = collect($hasil)->take(10);
$paging = $page->toJson();

或者您可以使用forPage($pageNumber, $pageCount)方法

$informasi = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $info);
$xml = new SimpleXMLElement($informasi);
$data = $xml->tki_data;
$pagess = $xml->total_page;
$hasil = array($data);
$page = collect($hasil)->forPage(1,10);
$paging = $page->toJson();