1

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.

1 Answers1

1

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
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
  • @AANoman ok, I can't check the ```name``` null or value in my route? – Yshai Siboni May 04 '20 at 11:38
  • @AANoman It was helpful but still dosn't work and It display the Error 404, that's my view : ```laravel @if(isset($name)) my name : {{$name}} and mail {{$email}} @else my name : name missing and mail {{$email}} @endif ``` – Yshai Siboni May 04 '20 at 11:48
  • 1
    when `$name=null` then `dd($name, $email)` This may help you most. Or in your view first check `{{dd($name, $mail)}}` then you decision what you want – A.A Noman May 04 '20 at 11:51
  • @AANoman you are helpul but still error.. don't know what to do – Yshai Siboni May 04 '20 at 11:55
  • @@AANoman No, if I write ```http://localhost/recruit/public/users/hjksdhgk@gmail.com``` It dosn't work (404 Error), maybe it's because ```name``` is before ```mail``` and I can't leave it empty? – Yshai Siboni May 04 '20 at 12:05
  • 1
    I ask you before when `$name=null` then `dd($name, $email)`. If not solve you have to order your route mail then name then try. May be it will solve your problem. – A.A Noman May 04 '20 at 12:10
  • 1
    @AANoman OK, that works, now my Route is : ```laravel Route::get('/users/{email}/{name?}', function($email, $name = NULL){ return view('users', compact('email','name')); }); ``` – Yshai Siboni May 04 '20 at 12:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213086/discussion-between-a-a-noman-and-yshai-siboni). – A.A Noman May 04 '20 at 12:14