search

Home  >  Q&A  >  body text

Laravel: Show authenticated username in redirect status message

I want to include the authenticated username when the user logs in and have the application redirect the user to the applicable page. In this specific example, the user will be redirected to their authenticated home page and the status message should read "Welcome back, {{Name}}"

The current message shows the code instead of 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 will return the following (the image contains the user "role_as == '0'")

Are there any other ways to achieve the desired result?

P粉377412096P粉377412096496 days ago895

reply all(2)I'll reply

  • P粉821808309

    P粉8218083092023-09-17 12:14:13

    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 );
    
        }
    }

    reply
    0
  • P粉154228483

    P粉1542284832023-09-17 11:54:07

    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 should not use {{}} here as it only works on blade files.

    We also use . to concatenate strings and variables, such as 'Hello' . $name. Variables cannot be enclosed in quotes when you concatenate them.

    reply
    0
  • Cancelreply