Return in Pagination View from Array in Laravel / Paginate :: make ()

1

When I create a pagination manually, it is giving error, it does not create in the view the amount of exact elements as I determine.

On my controller:

public function getIndex(){
    $fileClass = FileClass::with(['FileServico'])
        ->where('status','=','PR')
        ->where('id_cliente','=',1)
        ->orderBy('id_file','DESC')
        ->get()
        ->toArray();

    foreach($fileClass as $fileArr){
        foreach($fileArr['file_servico'] as $file){
            $f[] = $file;
        }
    }

    return View::make('home')
        ->with('fileClass',Paginator::make($f, count($f),2));
}

In the view I normally iterate to print the data and it is printed, the pager is usually created at the bottom and makes the exact count as I determine, however, the table that is created does not bring the exact elements. / p>

@foreach($fileClass as $f)
    <tr>
        <td>{{ $f['id_file'] }}</td>
    </tr>
@endforeach

And the pager is right, it usually appears:

@section('pagination')
    <section id="pagination">
        {{ $fileClass->links() }}
    </section>
@stop

At the time of printing the number of elements per page it prints all the elements, below the pager is presented normally, but there is no pagination itself.

    
asked by anonymous 06.06.2014 / 01:14

1 answer

2

Query Builder I believe to be more practical:

 $resultado = DB::table('file_class')
    ->join('file_servico', 'file_class.id', '=', 'file_servico.id') 
    ->where('file_class.status','=','PR')
    ->where('file_class.id_cliente','=',1)
    ->orderBy('file_class.id_file','DESC')
    ->paginate(10);

The $resultado already exits paged without problems only assign, for its View::make :

return View::make('home')
        ->with('fileClass', $resultado);

A good practice would be to do this in a class and just call the method:

class RepFileClass
    public function toListPaginate()
    {           
        return 
            DB::table('file_class')
                ->join('file_servico', 'file_class.id', '=', 'file_servico.id') 
                ->where('file_class.status','=','PR')
                ->where('file_class.id_cliente','=',1)
                ->orderBy('file_class.id_file','DESC')
                ->paginate(10);
    }
}

References:

06.06.2014 / 01:59