0

I have a long paragraph as a description, in the summary, I want to show just part of that, the first 50 words. This description stored in the database.

So how can I show just the first 50 words of this description? I am using Laravel 5.5.

I call it like that:

<p class="text-danger"> {{$project->description}} </p>

5 Answers5

2

You can use str_limit()

Try like this:

{{ str_limit($project->description, 50) }}

Hope this helps you!

Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
  • It counts by characters, not by words. but it okay for me, thanks. –  Nov 28 '17 at 09:03
  • Glad i could help you! Accept it and close the issue! – Hiren Gohel Nov 28 '17 at 09:04
  • @Gist Try this: `{!! Str::words($project->description, 10,'....') !!}` Hope this is what you are looking for! But i think this works only in Laravel 4, not in L5. For now just try and look the result! – Hiren Gohel Nov 28 '17 at 09:20
  • For some reason `str_limit`didn't work for me as the function has been deprecated [read it here](https://stackoverflow.com/questions/51095567/limit-text-using-str-limit-function-in-laravel-5-5), I had to use `Str::limit` but I'll use `Str::words` (seems better for what I'm tryin to do) – maiakd Jan 09 '22 at 11:52
1

do like this

 {{ strlen($project->description) > 50 ? substr($project->description,0,50).'..' : $project->description }}
Mohamed Akram
  • 2,244
  • 15
  • 23
1

Now we can use

{{ \Illuminate\Support\Str::words($project->description, 50 ) }}

to show the first 50 words in Laravel

Aloksinghse
  • 430
  • 3
  • 5
0

You can use array_splice:

{{ implode(' ', array_splice(explode(' ', $project->description), 0, 50)) }}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

In your model use

public function getShortContentAttribute()
    {
        return substr($this->content, 0, random_int(60, 150)). '...';
    }

in view use like below

{{ $article->ShortContent }}
HemalHerath
  • 1,006
  • 2
  • 18
  • 38