2017-02-15 58 views
1

我正在使用laravel 5.3,並且我想在用戶啓動網站時向用戶顯示一條消息。在我main.blade.php我寫了下面的代碼爲我的網頁Laravel 5.3中的Flash消息

 @if(Session::has('message')) 
     div class="alert"> 
     {{Session::has('message')}} 
     </div> 
     @endif 

上得到的消息在我的控制器功能,我想用戶重定向到我的主頁爲此,我寫下了下面的代碼

return Redirect::to('/')->with('message','you have singup successfully please login'); 

現在,在我的主頁上,一個div帶有各自的風格,但消息不會進入此div。而不是消息「1」即將在此div中。 任何解決方案或教程將是您最好的期望。 Regards

+0

會議::閃光燈;( '消息', '你有singup成功請登錄。')保持這樣並檢查 – Sona

+0

我必須在用戶註冊後顯示此消息。 –

+0

通過在控制器根據條件它將來到 – Sona

回答

2

您應該使用session::has(),has函數只是檢查它是否存在。要使用會話中的值,請使用session::get()

此代碼應爲你服務好:

<div class="alert"> {!! Session::get('message') !!} </div>

如果你想在消息只顯示一次,你可以閃光的會話。

而不是使用Redirect::to('/')->with('x','y'); 的您可以使用 Session::flash('message', 'This message will be shown once!');但你必須這樣做重定向之前添加的代碼,你會不會需要with()了。

有關Laravel會話的詳細信息看看文檔:https://laravel.com/docs/5.4/session

+0

感謝@Arnold它的工作,但它不會消失,直到我刷新頁面。 –

+0

後纔出現。 –

+0

更確切地說,我說, 我如何設置消息的消息時間段 –

1
Try this code: 

    In Controller: 

    if ($x->save()) { 
        return Redirect::route('/')->with('success', 'Added Successfully!!'); 
       } else { 
        return Redirect::route('/')->with('fail', 'Failed'); 
       } 

    View Page: 

    @if(Session::has('success')) 
             <div class="alert alert-success">{{Session::get('success')}}</div> 
             @elseif(Session::has('fail')) 
             <div class="alert alert-danger">{{Session::get('fail')}}</div> 
             @endif 
+0

謝謝@Soniya我已經嘗試過,它完全正常工作。 –

+0

快樂是我的.. @ Qadeer_Sipra – Sona

+0

我有評價:) @sona –