0

问题

如何限制 Eloquent 结果中的某一行?

设想

我只需要从结果中的一个字段中检索大约 100 个字符。目前我正在使用 join 语句,因此返回了多个结果。我基本上只需要前 100 个字符post.content

代码

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();
    }

我不确定如何对查询进行过滤以仅返回 100 个字符。我环顾四周,但没有发现任何有用的东西,至少对我的具体情况没有。

4

1 回答 1

0

目前无法对此进行测试(抱歉),但如何:

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;
}
于 2017-01-30T13:39:06.297 回答