How to use custom blade directive in Laravel

I am trying to use the argument passed in the directive to calculate the condition and return a string like this

Blade::directive('resource', function($page) {
    return (strcmp(URL::full(), URL::to("/admin/" + $page)) == 0) ? "active" : "inactive";
});

If the page name that has passed into the field is evaluated to the full URL, then it returns the active string, otherwise return inactive. For styling

The problem is that $ page evaluates (value), where value is the string passed inside. Can I use this argument to calculate conditions here?

The closest post I could find is Using the Blade directive in the Blade directive , but no answer.

thank

+4
source share
1 answer

Try

Blade::directive('resource', function($page) {
    return "<?php echo '".((strcmp(URL::full(), URL::to("/admin/" + $page)) == 0) ? "active" : "inactive")."' ?>"
});
+1
source

Source: https://habr.com/ru/post/1599216/


All Articles