Home > front end >  Display Authenticated User's Name in Redirect Status Message - Laravel
Display Authenticated User's Name in Redirect Status Message - Laravel

Time:12-19

I want to include the authenticated user's name when the user log in and the application redirect the person to the applicable page. In this specific example the person is being redirected to their authenticated homepage and the status message should display "Welcome back, {{Name}}"

Currently the message displays the code and and not the actual value.

I have tried the following:

public function authenticated()
    {
        if(Auth::user()->role_as == '1') //Admin = 1
        {
            return redirect('admin/dashboard')->with('status', 'Welcome to your Admin Dashboard, {{ Auth::user()->name }}.');
        }
        else
        {
            return redirect('/home')->with('status', 'Welcome back,' . " " . '{{ Auth::user()->name }}');

        }
    }

This returns the following (image contains user with "role_as == '0'") :

enter image description here

What other method is there to get to the desired result?

CodePudding user response:

Try this:

public function authenticated()
    {
        if(Auth::user()->role_as == '1') //Admin = 1
        {
            return redirect('admin/dashboard')->with('status', 'Welcome to your Admin Dashboard, '. Auth::user()->name .'.');
        }
        else
        {
            return redirect('/home')->with('status', 'Welcome back,' . Auth::user()->name);

        }
    }

You shouldn't use {{}} here bacause this only works for blade files.

Also we use . to concat strings and variables like 'Hello' . $name. When you're concating them the variable can't be in quotations.

CodePudding user response:

public function authenticated()
{
    if(Auth::user()->role_as == '1') //Admin = 1
    {
        return redirect('admin/dashboard')->with('status', 'Welcome to your Admin Dashboard, ' . Auth::user()->name . '.');
    }
    else
    {
        return redirect('/home')->with('status', 'Welcome back, ' . Auth::user()->name );

    }
}
  • Related