簡體   English   中英

Laravel 5.3中的Flash消息

[英]Flash Message in Laravel 5.3

我正在使用laravel 5.3,我希望在用戶單獨登錄網站時向用戶顯示一條消息。 在我的main.blade.php中,我編寫了以下代碼,用於在我的頁面上獲取消息

     @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。 任何解決方案或教程都是您最好的期待。 問候

你應該使用session::has()has函數只是檢查它是否存在。 要在會話中使用session::get() ,請使用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 Sessions的更多信息,請查看文檔: https ://laravel.com/docs/5.4/session

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM