简体   繁体   English

在创建Paginator实例时将原始选择语句传递给laravel Eloquent

[英]Pass raw selection statement to laravel Eloquent while creating a Paginator instance

Pagination in Laravel is super easy. 在Laravel中分页非常容易。 I simply go: 我只是去:

$posts = Post::where( .... );
$posts->paginate(20, ['ID', 'title', 'body'], ....);

I am trying to replace the ID,title,body array with: ID, title, LEFT(body, 200) But Laravel doesn't accept that! 我正在尝试将ID,title,body数组替换为: ID, title, LEFT(body, 200)但是Laravel不接受! I found out that I should use this instead: 我发现我应该改用这个:

connection()->raw('ID, title, LEFT(body, 200) AS body');

For this to work, but the paginate method only accepts an array. 为此,但paginate方法仅接受一个数组。 Is there a way around it? 有办法解决吗?

Do this: 做这个:

$posts = Post::where( .... );
$posts->paginate(20, ['ID', 'title', \DB::raw('LEFT(body, 200) AS body')], ....);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM