laravellaravel-bladelaravel-routinglaravel-views

How to display one of two options for parameter in view file laravel


I created a view file with the syntax : my name: {{$name}} and mail {{$email}} and I need to create a route with 2 parameters (one optional and one required) and this will be the sentence that will be shown : my name : ["name" or "name missing"] and mail ["email"] but I can't display the second option (when the name paramter isn't included), that's my route ;

 Route::get('/users/{name?}/{email}', function($name = NULL, $email){
    if(isset($name)){
        return view('users', compact('name','email'));
    }
    else {
        return view('users', compact(missing name,'email'));
    }
});

I know that my problem is in the else but I don't know what to write inside.


Solution

  • In your view file you have to check name null or value.

    Route::get('/users/{email}/{name?}', function($email, $name = NULL){
    
        return view('users', compact('name','email')); 
    });
    

    And your view check

    @if(isset($name))
        //do something
    @else
        // Do something
    @endif