Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.1 kB
4
Indexable
Never
public function table_data_mbr_kk_existing(Request $request)
    {
         
        $columns = array( 
            0 =>'no_kk',
            1 =>'no_kk',
            2 =>'alamat',
            3 =>'no_rt', 
            4 =>'no_rw',  
            5 =>'kelurahan',  
            6 =>'kecamatan',          
            
        );

        $q = DB::table('dinsos_mbr')->select('no_kk','alamat','no_rt','no_rw','kelurahan','kecamatan')->where('ket_sk','1')->whereNull('bantuan_all')->where('periode',$request->bulan)->where('tahun',$request->tahun);


        if ($request->kelurahan!='0') {
            $q = $q->where('kelurahan',$request->kelurahan);
        }

        if ($request->kecamatan!='0') {
            $q = $q->where('kecamatan',$request->kecamatan);
        }
        if ($request->rw!='0') {
            $q = $q->where('no_rw',$request->rw);
        }

        if ($request->rt!='0') {
            $q = $q->where('no_rt',$request->rt);
        }
        
        $q = $q->distinct('no_kk');
        $totalData = count($q->get());
        $totalFiltered = $totalData; 

        $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 = $q->offset($start)
            ->limit($limit)
            ->orderBy($order,$dir)
            ->get();
        }else {
            $search = $request->input('search.value'); 

            $posts = $q->where('no_kk','LIKE',"%{$search}%")
                    ->distinct('no_kk')
                    ->offset($start)
                    ->limit($limit)
                    ->orderBy($order,$dir)
                    ->get();

           
            $totalFiltered = $q->where('no_kk','LIKE',"%{$search}%")
                            ->distinct('no_kk')
                            ->count();
        }

        $data = array();
        if(!empty($posts))
        {
            foreach ($posts as $post)
                {
                    $nestedData['anggota'] = $this->getAnggota($post->no_kk,$request->bulan,$request->tahun);
                    $nestedData['no_kk'] = get_sensor($post->no_kk);
                    $nestedData['alamat'] = $post->alamat;
                    $nestedData['rt'] = $post->no_rt;
                    $nestedData['rw'] = $post->no_rw;
                    $nestedData['kelurahan'] = $post->kelurahan;
                    $nestedData['kecamatan'] = $post->kecamatan;
                    
                    $data[] = $nestedData;

            }
        }

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

        echo json_encode($json_data);
    }