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..

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

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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!

3 Comments

You really should try to avoid putting view (formatting) related logic inside your model, otherwise you'll end up with some very bulky models.
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
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.