Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel using paginate with only selected columns?

I know how to use paginate in laravel , but now I want use paginate() with selected columns only . this is my pagination code:

$data['users']=DB::table('users')->paginate(2);

this the sql statement I'm trying to get with paginate.

select id,name,username,email,groupName,lastSignIn from users;

my laravel code for this select statement:

$data['users']=DB::select('select id,name,username,email,groupName,lastSignIn from users;')->paginate(4);

but it's not working with me?

like image 265
Osama Al-Banna Avatar asked Sep 20 '14 12:09

Osama Al-Banna


Video Answer


2 Answers

You may try this snippet code:

$user = User::paginate(5,['id','name','username'.....]);
like image 114
Nadeem Ijaz Avatar answered Oct 11 '22 13:10

Nadeem Ijaz


This is what you need:

// Query\Builder
DB::table('users')
   ->select('id', 'name', 'username', 'email', 'groupName', 'lastSignIn')
   ->paginate(4);

And this:

DB::select(...);

is completely different method of another class (Connection).

like image 34
Jarek Tkaczyk Avatar answered Oct 11 '22 12:10

Jarek Tkaczyk