Я использую realrashid / sweet-alert версии 3.1 с laravel 5.8
Проблема в том, что когда я использую оповещение после успешного завершения формы или после сбоя проверки, здесь не работает мой код
public function store(Request $request)
{
$rules = [
'title' => 'required',
'title_desc' => 'required',
'desc' => 'required',
'pic' => 'required',
];
$customMessages = [
'title_desc.required' => 'The Short Description field is required.',
];
$validator = Validator::make($request->all(), $rules, $customMessages);
if ($validator->fails()) {
Alert::error('error', 'Validation Errors');
return back();
}
$update_arr = array(
'title' => "hello",
'description' => "world",
'title_desc' => "hello world",
'added_on' => date("Y-m-d H:i:s")
);
StoriesModel::create($update_arr);
Alert::success('Success', 'Story Created Successfully');
return redirect('admin/stories');
}
в этом коде я использовал предупреждение два раза, и с другой стороны, оно не работает при обоих условиях
, если я использую его, как показано ниже
public function create()
{
Alert::error('error', 'Validation Errors');
$this->data['title'] = 'Stories';
$this->data['menu'] = 'stories';
return View::make('admin.stories.create', $this->data);
}
Работает нормально
Вот мой главный макет
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>@yield('title')</title>
<link href="{{ url('assets/admin/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ url('assets/admin/font-awesome/css/font-awesome.css') }}" rel="stylesheet">
<!-- Toastr style -->
<link href="{{ url('assets/admin/css/plugins/toastr/toastr.min.css') }}" rel="stylesheet">
<!-- Gritter -->
<link href="{{ url('assets/admin/js/plugins/gritter/jquery.gritter.css') }}" rel="stylesheet">
<link href="{{ url('assets/admin/css/animate.css') }}" rel="stylesheet">
<link href="{{ url('assets/admin/css/style.css') }}" rel="stylesheet">
@yield('page_css_tags')
@yield('page_custom_css')
</head>
<body>
<div id="wrapper">
@include('layouts.admin.sidebar')
<div id="page-wrapper" class="gray-bg">
@include('layouts.admin.topbar')
@include('layouts.admin.content_header')
@yield('content')
@include('layouts.admin.footer')
</div>
</div>
<!-- Mainly scripts -->
<script src="{{ url('assets/admin/js/jquery-3.1.1.min.js') }}"></script>
<script src="{{ url('assets/admin/js/popper.min.js') }}"></script>
<script src="{{ url('assets/admin/js/bootstrap.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/metisMenu/jquery.metisMenu.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/slimscroll/jquery.slimscroll.min.js') }}"></script>
<!-- Flot -->
<script src="{{ url('assets/admin/js/plugins/flot/jquery.flot.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/flot/jquery.flot.tooltip.min.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/flot/jquery.flot.spline.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/flot/jquery.flot.resize.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/flot/jquery.flot.pie.js') }}"></script>
<!-- Peity -->
<script src="{{ url('assets/admin/js/plugins/peity/jquery.peity.min.js') }}"></script>
<script src="{{ url('assets/admin/js/demo/peity-demo.js') }}"></script>
<!-- Custom and plugin javascript -->
<script src="{{ url('assets/admin/js/inspinia.js') }}"></script>
<script src="{{ url('assets/admin/js/plugins/pace/pace.min.js') }}"></script>
<!-- jQuery UI -->
<script src="{{ url('assets/admin/js/plugins/jquery-ui/jquery-ui.min.js') }}"></script>
<!-- GITTER -->
<script src="{{ url('assets/admin/js/plugins/gritter/jquery.gritter.min.js') }}"></script>
<!-- Sparkline -->
<script src="{{ url('assets/admin/js/plugins/sparkline/jquery.sparkline.min.js') }}"></script>
<!-- Sparkline demo data -->
<script src="{{ url('assets/admin/js/demo/sparkline-demo.js') }}"></script>
<!-- ChartJS-->
<script src="{{ url('assets/admin/js/plugins/chartJs/Chart.min.js') }}"></script>
<!-- Date range use moment.js same as full calendar plugin -->
<script src="{{ url('assets/admin/js/plugins/fullcalendar/moment.min.js') }}"></script>
@yield('page_script_tags')
@yield('page_custom_script')
@include('sweetalert::alert')
</body>
</html>
Вы можете видеть, что я добавил @include ('sweetalert :: alert') до окончания тегов тела
и вот лезвие что расширяет мастер
@extends("layouts.admin.master")
@section('title')
Butterfly | Stories
@endsection
@section('content_header_title')
Stories
@endsection
@section('page_css_tags')
<link href="{{ url('assets/admin/css/plugins/blueimp/css/blueimp-gallery.min.css') }}" rel="stylesheet">
<link href="{{ url('assets/admin/css/plugins/select2/select2.min.css') }}" rel="stylesheet">
<link href="{{ url('assets/admin/css/plugins/select2/select2-bootstrap4.min.css') }}" rel="stylesheet">
@endsection
@section('content')
<div class="wrapper wrapper-content">
<div class="row">
<div class="col-md-12">
<div class="ibox">
<div class="ibox-title">
<h5>{{ @$record ? "Edit" :'Create New' }} Story</h5>
</div>
<div class="ibox-content">
<form method="post" action="{{url('admin/store_story')}}"
enctype="multipart/form-data" role="form">
@csrf
<div class="form-group">
<select class="select2_demo_1 form-control" id="select_seaction" name="seaction"
required>
<option value="" disabled selected>Select Seaction</option>
<option value="header">Header</option>
<option value="middle">Middle Content</option>
<option value="content">Footer Content</option>
</select>
</div>
<div class="form-group">
<label for="title">Title</label>
<input type="text" placeholder="Enter Title" class="form-control" id="title" autofocus
value="{{ @$record ? $record->title : old('title') }}" name="title" required>
</div>
<div class="form-group">
<label for="title_desc">Short Description</label>
<input type="text" placeholder="Enter Short Description" class="form-control"
id="title_desc" value="{{ @$record ? $record->title_desc : old('title_desc') }}"
name="title_desc" required>
</div>
<div class="form-group">
<label for="desc">Story</label>
<textarea class="form-control" name="desc"
id="desc"
rows="5"
required>{{ (@$record) ? $record->description: old('desc') }}</textarea>
</div>
<label title="Upload image file" for="inputImage" class="btn btn-default">
<input type="file" accept="image/*" name="pic"
id="inputImage" required>
Upload image
</label>
<div class="lightBoxGallery" id="imageUpload_display">
@if(@$record)
<a href="{{asset(@$record->image_path)}}"
title="{{ @$record ? $record->title : old('title') }}" data-gallery="">
<img src="{{asset(@$record->image_path)}}">
</a>
@endif
<!-- The Gallery as lightbox dialog, should be a child element of the document body -->
<div id="blueimp-gallery" class="blueimp-gallery">
<div class="slides"></div>
<h3 class="title"></h3>
<a class="prev">‹</a>
<a class="next">›</a>
<a class="close">×</a>
<a class="play-pause"></a>
<ol class="indicator"></ol>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-primary btn-sm" type="submit">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('page_script_tags')
<script src="{{ url('assets/admin/js/plugins/blueimp/jquery.blueimp-gallery.min.js') }}"></script>
<!-- Select2 -->
<script src="{{ url('assets/admin/js/plugins/select2/select2.full.min.js') }}"></script>
@endsection
@section('page_custom_script')
<script>
$(document).ready(function (event) {
$(".select2_demo_1").select2({
theme: 'bootstrap4',
});
});
</script>
@endsection