Present summary content in Laravel 5.1

Asked

Viewed 1,138 times

3

I have a controller that performs a paged query:

$noticias = Noticias::orderBy('created_at', 'desc')->paginate(4);

However, one of the returned columns is the content. The column in question has a lot of text, as well do in Laravel so that it returns only a summary containing the first 380 characters or so the first 200 words?

2 answers

4


In the Laravel there is a resource that allows us to return a value, in a model property, as if it were in the table.

For example, if you have the field conteudo in your table, you will be able to make the magic property conteudo_resumido is returned from that original content.

We can do this by adding a method in Model, which starts with get, followed by the name with Studlycase, and then with the word attribute. That is to say:

MeuModel::getConteudoResumidoAttribute()

Example:

class Post extends \Eloquent
{

    protected $appends = ['conteudo_resumido'];
    public function getConteudoResumidoAttribute()
    {
        return str_limit($this->getAttribute('conteudo'), 200, '...');
    }
}

From this, we can do the following in the call of our model Post:

@foreach($posts as $post)
   <li>{{ $post->conteudo_resumido }}
@endforeach

I used the property appends to determine that the magic method will be loaded along with the result brought from a common query, as in case you need it in the json, it will already be automatically returned.

Updating

If you are facing problems with broken HTML tags because of the "truncate" generated by the function str_limit, just change the method described above by placing the function strip_tags before the use of str_limit.

str_limit(strip_tags($this->getAttribute('conteudo')), 200);

Thus, only text characters, not tag characters, will be counted, and you will not have problems with broken HTML tags.

-1

I managed to do so:

{{ preg_replace('/(<.*?>)|(&.*?;)/', '', \Illuminate\Support\Str::words($noticia->conteudo, 70, "...")) }}

With this I can remove the html tags and special formatting, as well as limit to a total of 70 words before inserting the three points, elements that represent the continuity of the content.

  • Inserting that kind of logic into the view is not a good idea. If I need this same summary in another view of the application I need to duplicate this huge logic?

  • yes indeed, but if I have to do this in the controller I will have to overwrite that array position, coreto?

  • See the @Wallace response. It’s already explaining very well how to solve this type of problem

  • 1

    If the tags were the problem, just say the words, I edited the questions. And why use preg_replace if there are already strip_tags?

  • Just one more thing, from the database is returned an array with the content, in your example I think you are reading only a string. To make the summary of everything I have to use a right loop? Or the Standard provides some resource for this?

  • I did not understand very well. How so the bank returns a array?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.