Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Paginate and get()

With the code below, what I wanted was paginate the query I created. But, when I try to add paginate after get, it throws an error. I wanted to remain get since I want to limit to columns that was set on $fields. What would should be the better idea to paginate this thing? or what's a good substitute for get and limit the columns?

What I tried:

->get($this->fields)->paginate($this->limit)

Part of my controller:

class PhonesController extends BaseController {

    protected $limit = 5;
    protected $fields = array('Phones.*','manufacturers.name as manufacturer');
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {



        if (Request::query("str")) {
            $phones = Phone::where("model", 'LIKE', '%'. Request::query('str') . '%')
                        ->join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
                        ->get($this->fields);

        } else {
            $phones = Phone::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
                            ->get($this->fields);
        }



        return View::make('phones.index')->with('phones', $phones);
    }

}

like image 469
Bajongskie Avatar asked Oct 27 '13 09:10

Bajongskie


People also ask

How can I get pagination in Laravel?

There are several ways to paginate items. The simplest is by using the paginate method on the query builder or an Eloquent query. The paginate method provided by Laravel automatically takes care of setting the proper limit and offset based on the current page being viewed by the user.

How does Laravel pagination work?

Pagination works by selecting a specific chunk of results from the database to display to the user. The total number of results is calculated and then split between pages depending on how many results you want to return on a page.

What is cursor pagination in Laravel?

Cursor pagination is a high performant pagination technique often used for large data-sets, infinite scrolling and APIs (more on that later). Today's Laravel release adds the cursorPaginate method to the Eloquent and database query builder classes, which uses cursor pagination under the hood.


1 Answers

If you look at the method signature you will see that paginate receives a second argument, $columns. So your solution would be to use

->paginate($this->limit, $this->fields);

Furthermore, you can clean up your controller by changing things slightly:

public function index()
{

    $query = Phones::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id');

    if ( Request::query('str') ) {
        $query->where('model', 'LIKE', '%'. Request::query('str') . '%')
    }

    $phones = $query->paginate($this->limit, $this->fields);

    return view('phones.index')->with('phones', $phones);
}
like image 181
William Cahill-Manley Avatar answered Sep 30 '22 11:09

William Cahill-Manley