1

I have a problem with helpers , I create correctly the helper and I can call this helper that I created in view but when I need to access to property.

{{Text::showBanner()}};

The function of helperI created called showBanner

 public static function showBanner() {
        $banner= Banner::all();

        return $banner;
    }

How can I access to attribute id of $banner ?

UPDATED

When I use this

  @foreach ( $banners as $item)
                        {{$item}}
                       @endforeach

I need to change this $banner for this helper

4
  • Banner::all() gives you all banners. A collection and not a single model. That's probably the problem here. Commented Aug 9, 2015 at 15:54
  • Yes , I know , how can I put this Text::showBanner in a function Helper @lukasgeiter Commented Aug 9, 2015 at 16:28
  • @jcsa look at this pls and let me know if it helps stackoverflow.com/questions/31665553/… Commented Aug 9, 2015 at 18:40
  • Yes all of their answers helps to me , but my problem is when i try to use the {{Text::showBanner()}} in the foreach replacing the variable $banners. Commented Aug 9, 2015 at 19:14

1 Answer 1

1

create a file app/helpers.php with your desired code:


    function showBanner() {
        $banner = Banner::all();
        return $banner;
    }

Autoload it with composer


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

use it {{ showBanner() }}

Sign up to request clarification or add additional context in comments.

4 Comments

I can use , I updated the question with problem @tudor
$item is an instance of Banner entity. You can it in blade templating engine like you use it in php. You can call methods on it {{ $item->getLink() }} or access public properties.
Yes I know but I need to use the Helper function in thi foreach @tudor
you can do this but it is a wrong way to do it: @foreach(Text::showBanner() as $item) { // your code here}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.