mercredi 16 novembre 2016

How to Use Function inside @foreach - Laravel

Thanks For Reading. I'm new in Laravel, i want to try to change the output of Database with @foreach blade, there is the example :

This is Route :

Route::get('/home', 'warnajati@index');

This is Controller :

public function index()
{
    $post = DB::table('posts')->get();
    return view('warnajati', ['posts'=>$post]);
}

This is Views :

 @foreach ($posts as $post)
   <div class="title"><h3></h3></div>
 @endforeach

with Output of $post->title is "This is The Looonger Title you ever know" , and i want to make the title is shorter with Wordlimit() function i have made :

function wordlimit($text, $limit=10)
{
    if (strlen($text)>$limit) {
        # code...
        $word = mb_substr($text,0,$limit-3)."...";
    }else{
        $word =$text;
    }
};

How and Where i must place that function in laravel Project ?? please help me..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire