简体   繁体   English

Laravel 4:如何使用雄辩的ORM / blade显示帖子的预览

[英]Laravel 4: how to display a preview of a post using eloquent ORM/blade

I built a simple cms in Laravel 4. At the moment the main site index shows a list of all posts including the body of the post. 我在Laravel 4中构建了一个简单的cms。此刻,主站点索引显示了包括帖子正文在内的所有帖子的列表。

I want to only show, for example, the first 100 letters of the body of my post as a preview before viewing the whole article on my show.blade.php page. 例如,在查看show.blade.php页面上的整篇文章之前,我只想显示帖子正文的前100个字母作为预览。

The relevant part of the index.blade looks like this: index.blade的相关部分如下所示:

@foreach($posts as $post)
...
<p>{{ $post->body }}</p>

And the relevant part of my DisplayController looks like this: 我的DisplayController的相关部分如下所示:

public function index()
{
    $posts = $this->post->orderBy('id', 'DESC')->get();

    return View::make('index', compact('posts'));
}

How would I display only the first 100 characters of each post on my index page (as an example)? 如何在索引页面上仅显示每个帖子的前100个字符(例如)?

@foreach($posts as $post)
     <p>{{ substr($post->body, 0, 100) }}</p>
@endforeach

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

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