0

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>{{$post->title}}</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..

  • Your function has no return value... Laravel already has this function: https://laravel.com/docs/5.3/helpers#method-str-limit – Devon Bessemer Nov 16 '16 at 14:54
  • Where is your wordlimit function? You might be able to use the function as you normally would, depending on the function. – aynber Nov 16 '16 at 14:57

3 Answers3

3

Your function has no return value... Laravel already has this function: http://laravel.com/docs/5.3/helpers#method-str-limit

 @foreach ($posts as $post)
   <div class="title"><h3>{{ str_limit($post->title, 10) }}</h3></div>
 @endforeach
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
2

You can use Laravel's Accessor for doing that like this inside a Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function getShortTitleAttribute($value)
    {
        // return shortened title here ...
    }
}

and then you can use it in blade like this:

{{ $post->short_title }}

Hope this helps!

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45
  • You really should try to avoid putting view (formatting) related logic inside your model, otherwise you'll end up with some very bulky models. – Devon Bessemer Nov 16 '16 at 14:58
  • I know but its also not the bad way, as if we don't find the logic like `str_limit` we can do using this too..Its just the Laravel Way for solving complex things and make the code readable and cean – Saumya Rastogi Nov 16 '16 at 15:01
  • The Laravel way involves separation of concerns. This would be violated if mutators are being used for display formatting. In my mind, setting a string limit like this only makes sense in the context of the view. – Devon Bessemer Nov 16 '16 at 15:02
0

You can put your function in helpers.php file from libraries folder. Just make sure that you have helpers.php file autoloaded in composer.json file:

"autoload": {
    "files": [
        "libraries/helpers.php"
    ],
},

If you had to add this to your composer.json you will also have to run composer dump-autoload command from terminal.

For more info check out Best practices for custom helpers on Laravel 5.

Community
  • 1
  • 1
MBozic
  • 1,132
  • 1
  • 10
  • 22