skip to Main Content

I am working in Laravel and facing an issue about creating route.

this is my blade file code kindly check anchor tags.

<ul class=" dropdown-menu">
@foreach ($category as $categoryItem)
<li>
<a
href="{{ route('shop', ['slug' => $categoryItem->slug]) }}">
{{ $categoryItem->name }}
<span class="caret"></span>
</a>

@if ($categoryItem->subcategories->isNotEmpty())
<ul class="dropdown-menu">
@foreach ($categoryItem->subcategories as $subcategoryItem)
<li>
<a href="{{ route('shop', ['categorySlug' => $categoryItem->slug, 'slug' => $subcategoryItem->slug]) }}"
data-toggle="dropdown"
class="dropdown-toggle">{{ $subcategoryItem->name }}
<span class="caret"></span>
</a>

@if ($subcategoryItem->childcategory->isNotEmpty())
<ul class="dropdown-menu">
@foreach ($subcategoryItem->childcategory as $childCategoryItem)
<li>
<a
href="{{ route('shop', ['categorySlug' => $categoryItem->slug, 'subcategorySlug' => $subcategoryItem->slug, 'slug' => $childCategoryItem->slug]) }}">
{{ $childCategoryItem->name }}
</a>
</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif

</li>
@endforeach
</ul>

and this is route that i have created in web.php file

Route::get('shop/{categorySlug?}/{subcategorySlug?}/{slug?}', [ShopController::class, 'shop'])->name('shop')->where(['categorySlug', 'subcategorySlug', 'slug' => '[wd-]+(.*)']);

but with this code i am seeing floowing slugs for category, subcategory and child category respectively

http://127.0.0.1:8000/shop///mens-fashion
http://127.0.0.1:8000/shop/mens-fashion//men-clothes
http://127.0.0.1:8000/shop/mens-fashion/men-clothes/t-shirt--shirts-

as you can see extra slashes are coming in category and sub-category slugs which is not good at all I need only one

how can i resolve this issue with route method?

2

Answers


  1. Chosen as BEST ANSWER

    Route parameters in blade file should be exactly same name as i have defined in route file


  2. Your issue is that you are passing the wrong parameters to the calls to route:

    To clearly demonstrate the error, see the following tinker output on your route:

    ❯ tnkr
    Psy Shell v0.11.18 (PHP 8.1.19 — cli) by Justin Hileman
    > route('shop')
    = "http://tysyacha.test/shop"
    
    > route('shop', ['categorySlug' => 'category'])
    = "http://tysyacha.test/shop/category"
    
    > route('shop', ['slug' => 'category'])
    = "http://tysyacha.test/shop///category"
    

    You need to update your route calls in the blade file to:

    ...
    href="{{ route('shop', ['categorySlug' => $categoryItem->slug]) }}"
    ...
    href="{{ route('shop', ['categorySlug' => $categoryItem->slug, 'subcategorySlug' => $subcategoryItem->slug]) }}"
    ...
    href="{{ route('shop', ['categorySlug' => $categoryItem->slug, 'subcategorySlug' => $subcategoryItem->slug, 'slug' => $childCategoryItem->slug]) }}"
    ...
    

    Optional parameters are only omitted when IT and ALL paremeters after it are also omitted. If you define a "more specific" parameter for example slug, but omit subcategorySlug or categorySlug, then they will be added to the url as null, and (string) null === ''.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search