Laravel:在重定向狀態訊息中顯示經過驗證的使用者名
P粉377412096
P粉377412096 2023-09-16 13:45:05
0
2
799

我想在用戶登入時包含經過身份驗證的用戶名,並且應用程式將用戶重定向到適用的頁面。在此特定範例中,使用者將被重定向到其經過驗證的主頁,狀態訊息應顯示「歡迎回來,{{Name}}」

目前訊息顯示代碼而不是實際值。

我嘗試過以下方法:

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

        }
    }

這將傳回以下內容(圖像包含用戶 “role_as == '0'”)

還有什麼其他方法可以達到預期的結果?

P粉377412096
P粉377412096

全部回覆(2)
P粉821808309
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 );

    }
}
P粉154228483

試試這個:

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

        }
    }

您不應該在此處使用 {{}},因為它只適用於刀片檔案。

我們也使用 . 來連接字串和變量,例如 'Hello' 。 $名稱代码>。當您連接它們時,變數不能用引號引起來。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!