问题
如何对 Eloquent 结果中的某一行设置限制?
场景
我只需要从结果中的一个字段中检索大约 100 个字符。目前我正在使用 join 语句,因此会返回多个结果。我基本上只需要 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 个字符。我简单地环顾四周,但没有发现任何有用的东西,至少对我的特定场景没有用处。
最佳答案
目前无法测试这个(抱歉)但是怎么样:
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;
}
关于mysql - Laravel SUBSTRING 关于 Eloquent 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936286/