S
S
smilelan2020-02-13 09:48:36
Laravel
smilelan, 2020-02-13 09:48:36

How to shorten the text of the post on the main page?

The controller has this entry:

public function index()
    {
        $posts = Post::latest('published_at')->where('is_published','true')->limit(5)->get();
        return view('welcome',compact('posts'));
    }


how to use str::limit to display the text of the post (content) in an abbreviated form, I don’t know how to write the code correctly
Thank you in advance for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2020-02-13
@smilelan

Register in the Post model

public function getShortContentAttribute()
{
    return Str::limit($this->content, 20, ' (...)');
}

receive in template
$post->ShortContent;

Y
Yuri Kulaxyz, 2020-02-13
@Kulaxyz

This is done not in the controller, but in the blade itself. Read here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question