dimanche 25 novembre 2018

Limit words not characters in Laravel

I'm using Laravel 5.7 and was wondering what the correct way would be to limit the number of Words, not characters in a description I'm pulling from the Database and outputting in a blade/view.

I currently have this working by adding the following in my Blade file (Notice the class Str is in the blade/view):

@php use Illuminate\Support\Str; @endphp
{!! (nl2br(e(Str::words($test->testimonial, '25')))) !!}

The above limits my paragraph to 25 words, but I realize I should probably be using the Str class in my Controller not Blade.

When I add use Illuminate\Support\Str; in my controller and not the blade I get the error that Str is missing.

Here is my Controller function:

use App\Testimonial;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
etc...

public function index()
{
    $testimonial = Testimonial::all();
    return view('testimonials.index',compact('testimonial'));
}

How can I use the Str class in the Controller rather the Blade?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire