FatalErrorException: Error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
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?
You just need
User::paginate(15):
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 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.
I think it should be in the docs as I also ran into this issue when learning.
I agree this should be in the docs.
What about order? for example how could I run the following query? Items::all() ->orderBy('day', 'ASC') ->orderBy('level', 'ASC') ->paginate(10);
Remove the ::all() it should work as normal.
Items::orderBy('day', 'ASC')->orderBy('level', 'ASC')->paginate(10);
thanks! it solved my problem!
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();
}
}
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?
我也遇到类似 问题 我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();
awesome thanks , Bruno!!!