表单提交后显示消息 laravel

show message after form submit laravel

嗨,我是 Laravel 的初学者,我想在我的 Laravel 网站中使用 sweetalert2,但它没有显示在视图中

我使用来自 Github

的分支 dist

所以在我的 **master_layout.blade.php** 中,这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charest="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('title')</title>
    <link href="https://fonts.googleapis.com/css?family=Cairo&display=swap" rel="stylesheet">
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="css/jquery-ui.min.css" rel="stylesheet" type="text/css">
    <link href="css/jquery-ui.theme.min.css" rel="stylesheet" type="text/css">
    **<link href="css/sweetalert2.min.css" rel="stylesheet" type="text/css">**
    <link href="css/main_style.css" rel="stylesheet" type="text/css">
    <link href="css/responsive.css" rel="stylesheet" type="text/css">
    <link rel="icon" type="image/png" href="img/favorite-icon.png" />
</head>
<body>
    @include('include/Navbar')
    <main>

        @include('include/Sidebar')

        @include('include/HamburgerSidebar')

        <div class="container">
            @yield('Content')
        </div>
    </main>

    <script src="Js/jquery-3.4.1.min.js"></script>
    <script src="Js/popper.min.js"></script>
    <script src="Js/bootstrap.min.js"></script>
    <script src="Js/jquery-ui.min.js"></script>
    **<script src="Js/sweetalert2.all.min.js"></script>**
    <script src="Js/main-JQ.js"></script>
</body>
</html>

在我的控制器商店中:

public function store(Request $request)
    {
        $request_order=$request->section_order;
        if(empty($request_order)){
            $order_last = Section::latest('section_order')->get();
            if(empty($order_last)){
                $request_order=1;
            }else{
                $request_order=$order_last[0]['section_order']+1;
            }
        }

        $request->validate([
            'section_name' => ['required','min:3'],
            'section_pic' =>['image']
        ]);

        $image = $request->file('section_pic');

        if ( $image === null) {
            $image_name = 'default_img.jpg';
        }else{
            $image_name = rand(1, 20) . time() . $image->getClientOriginalName();
            $image->move(public_path('img'),$image_name);
        }

        $input_data = array(
        'section_order' => $request_order,
        'section_name' => $request->section_name,
        'section_pic' => $image_name
        );

        Section::create($input_data);

            return redirect('/Sections')->withSuccess('Success message');
    }

在我看来:

@if(Session::has('success'))
<script type="text/javascript">
   Swal.fire(
           'Good job!',
           'Successfully Saved!',
           'success'
   );
</script>
@endif

商店功能工作正常但在我看来不显示 sweetalert

我把所有 CSS 和 js 文件和图像文件夹都放在 public 文件夹中是否正确?

感谢@Joseph 他的评论解决了我的问题 我使用 window.onload 并且工作正常

@if(Session::has('success'))
  <script type="text/javascript">

  function massge() {
  Swal.fire(
            'Good job!',
            'Successfully Saved!',
            'success'
        );
  }

  window.onload = massge;
 </script>
@endif

控制器:

  session()->flash("success","Success Message");
  return redirect('/Sections');

Blade 文件

@if(session()->has('success'))
    <script type="text/javascript">
        $(function () {
            swal("Deleted!", "Data has been Deleted.", "success"),
                swal({
                    title: {{ session()->get('success') }},
                    text: "Your Custom or Dynamic SUccess message put here",
                    type: "success"})

        });
    </script>
@endif