Laravel 中的会话闪存消息超时

use*_*779 1 php laravel laravel-5

我使用控制器在 Laravel 页面中创建了 flash 消息。它显示良好,但需要在 flash 消息中添加超时

if($location_vaidation>0){
     $material_details->location_id=$requested_location;     
     }
     else{
        Session::flash('success', 'please fill the form with valid data');
        return Redirect::to('request');
        exit;           
     }  
Run Code Online (Sandbox Code Playgroud)

在视图页面

@if( Session::has("success") )
  <div class="alert alert-success alert-block" role="alert">
  <button class="close" data-dismiss="alert"></button>
  {{ Session::get("success") }}
 </div>
 @endif
 @if( Session::has("error") )
  <div class="alert alert-danger alert-block" role="alert">
  <button class="close" data-dismiss="alert"></button>
  {{ Session::get("error") }}
 </div>
 @endif
 <div class="flash-message"></div>
Run Code Online (Sandbox Code Playgroud)

小智 6

使用 Jquery 函数试试这个

$("document").ready(function(){
    setTimeout(function(){
       $("div.alert").remove();
    }, 5000 ); // 5 secs

});
Run Code Online (Sandbox Code Playgroud)


Cri*_*tea 6

在标题中添加以下内容:

<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@2.8.2/dist/alpine.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在 Blade 中使用它,如下所示:

@if (session($messageKey))
    <div x-data="{show: true}" x-init="setTimeout(() => show = false, 5000)" x-show="show">
        <div class="alert alert-success">
            {{ session($messageKey) }}
        </div>
    </div>
@endif
Run Code Online (Sandbox Code Playgroud)