Ranjeet's avatar

pagination in laravel 5

Iam unable to make pagination for following conditions.I think i cannot use paginate()function given by laravel for following conditions.How can i make pagination for this conditions .please help me... iam thinking to use array_slice() method.But how to display next items in page if i use array_slice()method.Or is their any easy to to do this

public function getReportsBasedOnDistrict() {

  $obj=App::make('App\Core\Data\Collection');
  $violenceTypes = $obj->typeOfViolence();
  $districts = $obj->district();

  $data = [];
  foreach ($districts as $district)
  {
    foreach($violenceTypes as $violenceType)
    {
      $sql = "SELECT count(*) FROM cases WHERE incident"."::json"."#>>"."'{typeOfViolence}'"." like "."'%$violenceType%' AND incident"."::json"."#>>"."'{district}'"."= '$district'";
      $records = \DB::select($sql);
      $data= array_merge_recursive($data, [
          "{$district}" => [
              "{$violenceType}" => $records[0]->count
          ] 
        ]);

    }
  }

 $data= array_map(function($d){
    return array_filter($d);
  }, $data);

$data=array_filter($data); return $data; } and my view is like this

foreach($record as $districts=>$violence) {{$districts}}

        @foreach($violence as $violenceType=>$counts)

        <td>{{$violenceType}}:{{$counts}}</td>

       @endforeach
   @endforeach
0 likes
2 replies
constb's avatar
constb
Best Answer
Level 6

@Ranjeet can you use GROUP BY and COUNT() in your query? I'm not sure about this 'json'-related stuff here, but you probably should do GROUP BY on both districts and violence_types and let database collect the counters. This will be more efficient and you're gonna be able to paginate with LIMIT inside the query. Otherwise you may convert the array into Collection and use it's forPage helper to extract relevant slice of data.

Ranjeet's avatar

ON USING GROUP BY IT GIVES ERROR

1 like

Please or to participate in this conversation.