1

The problem that I am having is that if I pass a value of an optional parameter in my get request, I get the same result that I would if no value was passed.

Route:

   Route::get('/play/game/{new?}', [
        'uses' => 'GameController@getGame',
        'as' => 'game'
    ]);

Controller:

public function getGame($new = null) {
    $quotes = Quote::orderBy('created_at', 'dec')->paginate(50);       

    if(!is_null($new)) {
        if ($new == 'yes')
            $newgame = true;
    } else {
         $newgame = false;
    }

    return view('game', ['quotes' => $quotes, 'newgame' => $newgame]);      
}

View:

@if($newgame ==true) 
 {{Session::flush()}}
@endif

@if($newgame == false)
<h1>Not a new game</h1>
@endif

The second if statement in the view is just a test, and it is this statement that always executes. For reference, I am using Laravel 5.2

2 Answers 2

1

You can register the route as follows:

Route::get('play/game/', [
        'uses' => 'GameController@getGame',
        'as' => 'game'
    ]);

and then when you call the url, pass the variable like this:

play/game/?new=yes

Then in your controller, you can check if the variable is set:

if($request->input('new')=='yes'

to check if the parameter is present:

$request->has('new')
Sign up to request clarification or add additional context in comments.

Comments

0

try this i think there is no need to put '?' symbol in parameter

Route::get('play/game/{new}', [
        'uses' => 'GameController@getGame',
        'as' => 'game'
    ]);

2 Comments

Actually, the ? symbol represents an optional parameter, so in the event that there is no value passed, Laravel wont go crazy and throw an error. You can read about it here: laravel.com/docs/5.1/routing
ohh sorry. even i don't about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.