Ad

Show Message After Form Submit Laravel

- 1 answer

hi I'm a beginner in Laravel I want to use sweetalert2 in my Laravel website but it did not display in the view

I use branch dist from Github

so in my **master_layout.blade.php** this is my code:

<!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 target="_blank" rel="nofollow noreferrer" href="https://fonts.googleapis.com/css?family=Cairo&display=swap" rel="stylesheet">
    <link target="_blank" rel="nofollow noreferrer" href="css/all.min.css" rel="stylesheet" type="text/css">
    <link target="_blank" rel="nofollow noreferrer" href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link target="_blank" rel="nofollow noreferrer" href="css/jquery-ui.min.css" rel="stylesheet" type="text/css">
    <link target="_blank" rel="nofollow noreferrer" href="css/jquery-ui.theme.min.css" rel="stylesheet" type="text/css">
    **<link target="_blank" rel="nofollow noreferrer" href="css/sweetalert2.min.css" rel="stylesheet" type="text/css">**
    <link target="_blank" rel="nofollow noreferrer" href="css/main_style.css" rel="stylesheet" type="text/css">
    <link target="_blank" rel="nofollow noreferrer" href="css/responsive.css" rel="stylesheet" type="text/css">
    <link rel="icon" type="image/png" target="_blank" rel="nofollow noreferrer" 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>

and in my controller store:

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');
    }

and in my view:

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

the store function works fine but not display sweetalert in my view

and I put all CSS and js file and images folder in public folder is that correct or not?

Ad

Answer

thanks to @ Joseph his comment solve my problem i use window.onload and it work fine

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

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

  window.onload = massge;
 </script>
@endif
Ad
source: stackoverflow.com
Ad