简体   繁体   English

雄辩的Laravel SUBSTRING

[英]Laravel SUBSTRING on Eloquent query

Question

How do I put a limit on one of the rows in an Eloquent result? 如何在雄辩的结果中对行之一设置限制?

Scenario 脚本

I need to retrieve only around 100 characters from one of the fields in my result. 我只需要从结果中的一个字段中检索大约100个字符。 Currently I'm using a join statement so multiple results are being returned. 目前,我正在使用join语句,因此将返回多个结果。 I basically need only the first 100 characters from post.content 我基本上只需要post.content的前100个字符

Code

public function getAll()
    {
        return Post::select('posts.id', 'posts.title', 'posts.content', 'posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
                    ->join('users', 'posts.user_id', '=', 'users.id')
                    ->get();
    }

I'm not sure how to go about putting a filter on the query to only return 100 characters. 我不确定如何在查询中放置过滤器以仅返回100个字符。 I've looked around briefly but I've not found anything useful, not to my specific scenario at least. 我环顾了一下,但没有发现任何有用的东西,至少对我的特定情况没有帮助。

Cant test this at the moment (sorry) but what about: 目前无法测试(抱歉),但是:

public function getAll(){
    $query = Post::select('posts.id', 'posts.title', 'posts.content','posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
                ->join('users', 'posts.user_id', '=', 'users.id')
                ->get();

    foreach($query as $entries){
        $entries->content = substr($entries->content, 1, 100);
    }

    return $query;
}

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

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