使用Laravel显示即时消息

我正在尝试使用Laravel弹出某种屏幕或显示flash消息,目前我已经建立了一条路由,并使用Ajax调用触发了该路由,该调用将字段提交给Controller方法以验证是否用户有任何违规行为,当点击“检查违规行为”按钮时,没有任何反应,我的页面也没有收到任何刷新消息。 这是我的看法:

@if(Session::has('success'))
  <script type="text/javascript">
     swal({
         title:'Info!',text:"{{Session::get('success')}}",timer:5000,type:'info'
     }).then((value) => {
       location.reload();
     }).catch(swal.noop);
 </script>
 @endif
<form action="{{ route('assignees.store') }}" enctype="multipart/form-data"  method="POST">
    @csrf

            <div class="row">
              <div class="col-xs-4 col-sm-4 col-md-4">
              <div class="form-group">
                <strong>Customer ID:</strong>
                <input class="form-control" type="text" name="custidno" id='cust' required autocomplete="off" onkeypress="myFunction()"  placeholder="Customer ID" >
                <button onclick="CheckViolation()"class="btn-info"type="button">Check for Violation</button>
                </div>
              </div>

JS代码


function CheckViolation()
  {
    var customerid= document.getElementById("cust").value;
    var url = "{{ url('violationcheck') }}";
    var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    }
  };
  xhttp.open("GET",url + "?" +"custidno=" + customerid,true);
  xhttp.send();
}

那是我的路线:

Route::get('violationcheck','ViolationController@violationcheck')->name('violationcheck');

这是我的基本控制器方法

public function violationcheck(Request $request)
{

  $custidno = customer::select("id")
  ->where('name',$request->custidno)
  ->first();
  $checked = DB::table('violations')
  ->select('severity',DB::raw('count(*) as total'))
  ->where("customer_id",$custidno->id)
  ->where("status",1)
  ->groupBy('severity')
  ->first();
  if(empty($checked))
  {
    $msg="No Violation found";
  }else{
      $msg="Violation found";

        }
        return redirect()->route('assignees.create')->with("success",$msg);

  }

我在做什么错了?

sqq_8712 回答:使用Laravel显示即时消息

尝试这个:

@if (session('info'))
    <div class="alert alert-info">
        {{ session('info') }}
    </div>
@endif

详细了解Redirecting With Flashed Session Data

本文链接:https://www.f2er.com/3163896.html

大家都在问