B
B
blackVirus2022-03-03 07:08:55
Laravel
blackVirus, 2022-03-03 07:08:55

How to write functions in index.blade.php in laravel?

I'm starting to learn laravel. I need to write a small function in one line in the index file. To check it later with @if ( ). So where should I write this function? Preferably not in the Controller :)
Well, or if in the Controller, then how to write foreach @if functions there?
Anyone who does not laugh at the fool, but will help I will be grateful)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Andreichenko, 2022-03-03
@blackVirus

It is better to move the logic to the controller, make a calculation and pass the result to the view, and then check it in the blade.
But also because blade is still a php file, it allows you to use php code, look towards the @php directive

S
Sergey delphinpro, 2022-03-03
@delphinpro

Create a file app/helpers.php(name and location doesn't matter)
In it, define the desired function(s).
Write it in composer.json

"autoload": {
        "psr-4": {
          ...
        },
        "files": [
            "app/helpers.php"
        ]
    },

Rebuild the autoloader
composer du
And then use this function anywhere, including in templates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question