Ignore optional parameter in Laravel?

43
July 12, 2018, at 02:00 AM

Currently within my routes file I've got the following Route defined:

Route::get('{country}/{county}/{city?}/{id}', 'AdsController@show')->name('show')->where('id', '[0-9]+');

And within the AdsController I've got the following function:

public function show($country, $region, $city = null, $id) {
    if($city !== null)
        echo "we do something here extra";
    echo "Let's run our normal function";
}

The only issue is it appears that I must pass the $city variable? for instance, if I access http://example.com/mycountry/myregion/mycity/1 it works fine? but if I was to run http://example.com/mycountry/myregion/1 it shows a 404 is their anyway around this other than creating multiple routes?

Answer 1

You would have to create two separate routes. Laravel won't interpret a blank slash (/) as a null variable.

You could also pass a zero ('0') value in the route URI (example: /mycountry/myregion/0/1).

Also, you could place the optional route parameters at the end of the route. That may help.

READ ALSO
Laravel: Trying to access value inside array in view template

Laravel: Trying to access value inside array in view template

So I'm trying to access the value of a few pair of key->values inside an array I create and pass to the view called config, I'd like to access to them like this : {{ $config->web_name['value'] }} or {{ $config->$web_name['value'] }} but both are returning...

14
Error on sending e-mail via SMTP

Error on sending e-mail via SMTP

This is my code for sending emailBut i am getting error and page keeps loading for long time

54
Use Xdebug with PhpStorm and WSL

Use Xdebug with PhpStorm and WSL

I use WSL (Ubuntu 1614) from Windows 10 as a web server with PHP 7

37
I have a table images and album_images. Is the album_images table unnecessary?

I have a table images and album_images. Is the album_images table unnecessary?

I've created a website where a user can upload a single image or a whole album of images and 3 views - one for displaying all single images, one for displaying all albums (just the title image of each album) and one for displaying all album_images that have album_id...

29