8

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

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Jason Spick
  • 6,028
  • 13
  • 40
  • 59

2 Answers2

9

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.

Community
  • 1
  • 1
Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33
0

One hackish way to do it is just embedding PHP in your blade template, at least in Laravel 4.0; I migrated an old project, poorly written, to laravel. Being so overwhelmed by the number of issues I ran into and time constraints I did not have time to look for a better way to do it. I did a bunch of html forms totaling about 30k lines of code.

<?php
 $haystack=Session::get('orderInfo.form.conditions',array());
?>

Then you can access your data normally:

{{in_array('Special Assignments',$haystack)?'checked="checked"':''}}

That's what worked for me.

NOTE: Just adding my 2 cents for the sake of documentation. There are better and cleaner ways to do it, as stated by the accepted answer.

angelcool.net
  • 2,505
  • 1
  • 24
  • 26