简体   繁体   中英

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.

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.

The relevant part of the index.blade looks like this:

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

And the relevant part of my DisplayController looks like this:

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)?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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