Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a static function in blade.php tempate in laravel 4?

I have a class public static method that grabs some database information and returns a integer. In a controller i can call that method just fine. How do i call that static method in a blade template?

For example:

@foreach ($tasks as $task)
  {{Task::percentComplete($task->id)}}%<br />
@endforeach

Thank you

like image 488
Jason Spick Avatar asked Jun 11 '13 19:06

Jason Spick


People also ask

Can we write PHP code in blade template Laravel?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.


1 Answers

You could either

A- Make it a Facade : http://laravel.com/docs/facades

B- Move it into a helper/library : https://stackoverflow.com/a/13481218/2446119

I personally think that helpers and libraries are much easier and quicker to code, but facades are cleaner.

like image 199
Alexandre Danault Avatar answered Sep 20 '22 15:09

Alexandre Danault