Laravel 使用仅选定列的分页?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25949037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:07:57  来源:igfitidea点击:

Laravel using paginate with only selected columns?

laravelpagination

提问by Osama Al-Banna

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

我知道如何在 laravel 中使用 paginate ,但现在我只想对选定的列使用 paginate() 。这是我的分页代码:

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

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

这是我试图通过 paginate 获得的 sql 语句。

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

my laravel code for this select statement:

我的这个选择语句的 Laravel 代码:

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

but it's not working with me?

但它不适合我?

回答by Nadeem Ijaz

You may try this snippet code:

你可以试试这个片段代码:

$user = User::paginate(5,['id','name','username'.....]);

回答by Jarek Tkaczyk

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).

是另一个类(Connection)的完全不同的方法。