simo
simo

Reputation: 24560

How to pass multiple arguments to custom blade?

As the directive method only takes $expression, is there an example about how to pass multiple arguments, without using explode ?

I tried:

Blade::directive('reportLink', function($expression, $expression2) {

And then:

@reportLink("a", "b")

But I got error, missing argument 2..

Upvotes: 1

Views: 2273

Answers (2)

Manish Champaneri
Manish Champaneri

Reputation: 652

You can use comma sepreted arguments like this

@reportLink($argument1, $argument1)
--stuff--
@endreportLink

You have to just explode the string at directive

Blade::directive('reportLink', function ($expression) {

    $expression = explode(',', $expression);

    return "<?php echo $expression[0].' '.$expression[1] ?>";
});

Upvotes: 0

Maurice
Maurice

Reputation: 1362

Came across this myself. Only way to do this is making it an array.

In your blade template:

@reportLink([$argument1, $argument1])
    --stuff--
@endreportLink

In your service provider:

Blade::directive('reportLink', function ($expression) {
    return "<?php App\ReportLink::YourMethod{$expression} ?>";
});

In your class:

class ReportLink
{
    public static function YourMethod($arguments)
    {
        dd($arguments); // this is now an array.
    }
}

Maybe you found this yourself already, but for future reference if people are searching for this.

Upvotes: 3

Related Questions