Skip to content
Advertisement

Laravel custom component parameter parsing issue

In my Laravel (7.x) application. I am trying to create a component for links:

Component:

<a class="links" href="{{ $route }}" title="{{ $title ?? null }}">
    @isset($icon)
        <i class="{{ $icon }}"></i>
    @endisset

    @isset($caption)
        <span>{{ $caption }}</span>
    @endisset
</a>

<x-link icon="{{ $icon }}" route="{{ route("admin.{$route}.create") }}" />
  OR
<x-link icon="{{ $icon }}" route="{{ route("admin." . $route. ".create") }}" />
  OR
<x-link icon="{{ $icon }}" route="{!! route("admin.{$route}.create") !!}" />
  OR
<x-link icon="{{ $icon }}" route="{!! route("admin." . $route. ".create") !!}" />

And getting this issue:

syntax error, unexpected token "endif", expecting end of file (View: path)

However, if I do this, then it works:

$url = route("admin.{$route}.create");

...

<x-link icon="{{ $icon }}" route="{{ $url }}"></x-link>

I am not a fan of declaring the variables just for single use, I like to pass the value directly. Therefore, I prefer the first method.

Why is this simple code not working..?

Advertisement

Answer

I found that the parameters work better, if the values are bind with them.

<x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" />

Additionally… In case, if the multiple values are required:

<x-link icon="{{ $icon }}" :route="route('admin.' . $route . '.create')" :params="[
    'key1' => 'value1',
    'key2' => [
        ...
    ],
]" />

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement