'Modal to show upon redirect (Laravel)

I am trying to get a modal to show upon redirect to a page.

I am following this response (Laravel how to redirect back to boostrap modal dialog window)

My controller:

return redirect('postings')->with('welcome_msg');

My view:

@if(Session::has('welcome_msg'))        
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif

<div class="modal fade" id="myModal role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    ...
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  </div>
</div>

However, the modal is not showing upon pop-up. What am I doing wrong?



Solution 1:[1]

<div class="modal fade" id="myModal role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    ...
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  </div>
</div>

@if(Session::has('welcome_msg'))        
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif

This may fix it. HTML has to load before javascript.

Solution 2:[2]

try this in your blade just before closing tag of </ body>

@if (session()->has('welcome_msg'))
    <script>
        $('#YourModalName').modal('toggle');
    </script>
@endif

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DIGITALCRIMINAL
Solution 2 Khalid Khan