framework icon indicating copy to clipboard operation
framework copied to clipboard

FatalErrorException: Error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()

Open arnigudj opened this issue 13 years ago • 12 comments

In Laravel 4, when I do

$users = User::all()->paginate(15); 

i get the following exeption: FatalErrorException: Error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()

This works tough:

$users = User::all();
$paginator = Paginator::make($users ->toArray(), $users ->count(), 15);

So I don't know if this is an issue or not... Any ideas?

arnigudj avatar Jan 30 '13 21:01 arnigudj

You just need

User::paginate(15):

brunogaspar avatar Jan 30 '13 21:01 brunogaspar

Awesome, thanks for that info! now it works :)

Maybe this should be added to the documentation. It says there:

$users = User::where('votes', '>', 100)->paginate(15);

That's why I thought I had to do the ::all() method.

arnigudj avatar Jan 30 '13 21:01 arnigudj

@arnigudj paginate() is one of the methods that turns a query object into some kind of result (either model or collection). And so is all(). Thus, you cannot chain these type of methods, while you can call paginate() after calling where(), which only modifies the query, but does not finish it.

franzliedke avatar Jan 30 '13 21:01 franzliedke

I think it should be in the docs as I also ran into this issue when learning.

mylesthedev avatar Jun 05 '13 00:06 mylesthedev

I agree this should be in the docs.

karimmaassen avatar Jul 31 '13 11:07 karimmaassen

What about order? for example how could I run the following query? Items::all() ->orderBy('day', 'ASC') ->orderBy('level', 'ASC') ->paginate(10);

samuraee avatar Aug 25 '13 07:08 samuraee

Remove the ::all() it should work as normal.

Items::orderBy('day', 'ASC')->orderBy('level', 'ASC')->paginate(10);

brunogaspar avatar Aug 25 '13 12:08 brunogaspar

thanks! it solved my problem!

ze00ro avatar Oct 11 '13 05:10 ze00ro

I get same error when I try this ...

$aphorisms = User::findOrFail($userId)->favorites->paginate(3);

the code is below

class FavoritesController extends \BaseController {


public function store()
{

    Auth::user()->favorites()->attach(Input::get('aforism-id'));


    return Redirect::back();
}


public function destroy($id)
{
    Auth::user()->favorites()->detach($id);
    return Redirect::back();
}

public function userFavorites($userId) {

    $aphorisms = User::findOrFail($userId)->favorites->paginate(3);
    //$paginator = Paginator::make($aphorisms->toArray(), $aphorisms->count(),3);


    $categories = Category::all();
    $favorites = DB::table('favorites')->whereUserId(Auth::user()->id)->lists('aphorism_id');

    return View::make('aforisme.favorite', compact('categories','aphorisms','favorites'))
        ->with('title', 'Aforisme-Cautare');
}

}


class User extends Eloquent implements UserInterface, RemindableInterface {

public function favorites(){
    return $this->belongsToMany('Aphorism', 'favorites')->withTimestamps();
}

}

androneo avatar Jan 29 '15 20:01 androneo

It might be the same issue when using it with 'all()' ?

Have you manage to fix the issue, or did something different to accomplish the expect result?

rapocalypse avatar Mar 10 '15 11:03 rapocalypse

我也遇到类似 问题 我demo$arr = DB::table('jd_users') ->join('tr_status','jd_users.userId','=','tr_status.orderNumber') ->select('jd_users.merchantorderid','jd_users.username','jd_users.usertel','jd_users.amount','jd_users.repayedtime','jd_users.stagenum', 'jd_users.checkintime','jd_users.modified','tr_status.orderstatus','jd_users.managertel','jd_users.manager') ->where(function($query) use ($request){ $query->where('jd_users.merchantorderid','<>',''); $query->where('jd_users.checkintime','<>',''); $query->where('jd_users.loanid','<>',''); $query->where('tr_status.orderstatus','<>','1'); if($request->input('username')){ $query->where('jd_users.username','LIKE','%'.$request->input('username').'%'); } if($request->input('merchantorderid')){ $query->where('jd_users.merchantorderid','LIKE','%'.$request->input('merchantorderid').'%'); } if($request->input('startOrder')) { $startArr = explode('-',$request->input('startOrder')); $checkInTime = $startArr[0].$startArr[1].$startArr[2]; //$query->where('left(jd_users.checkInTime,8)','=',$checkInTime); $query->where('jd_users.checkInTime','=',$checkInTime.'000000'); } //var_dump($request->all());

            if($request->all()['status'])
            {//判断是否有状态
                $query->where('tr_status.orderstatus','=',$request->all()['status']);
            }
            if($request->all()['code']){
                $query->where('jd_users.agencycode','=',$request->all()['code']);
            }

        })
        ->orderBy('modified','desc')->pageinate(15)->get();

siyangsihai avatar Apr 25 '17 03:04 siyangsihai

awesome thanks , Bruno!!!

Abdullah-Alig avatar Nov 07 '18 14:11 Abdullah-Alig