Pagination View return from Laravel/Paginate Array::make()

Asked

Viewed 1,408 times

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 do the iteration normally to print the data and the data is printed, the pager is normally created at the bottom and does the exact count as I determine, however, the table that is created does not bring back the exact elements.

@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

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

  • I solved my problem through this tutorial: https://arjunphp.com/laravel-5-pagination-array/ might be useful.

1 answer

2


Query Builder I believe it is 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 leaves paginated without problems just assign, to your 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:

  • But the second parameter is not total items??? If I make this change, it stops working, in the documentation it says that the amount per page is the third parameter, do you have any other recommendation? Thanks for the help.

  • Sorry I made a mistake... I’ll edit how your answer would be more or less ... there’s a better way but I’ll follow your way there!

  • @Ewertonmelo the way you’re doing this kind of wrong, like it’s going to be funny and I don’t think it’s cool, could it be done with Query Builder? if yes give me the name of the two tables that is a toothpick? or else Join in Eloquent also gives?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.