laravel Toastr显示但没有消息

时间:2019-06-27 14:00:23

标签: laravel notifications toastr

我正在尝试在用户填写表单而没有任何验证错误时显示成功消息。我正在使用Toastr js插件。 我已经将它们放在layout.blade.php文件中:

@toastr_css
@jquery
@toastr_js
@toastr_render
 @if(Session::has('message'))
    var type = "{{ Session::get('alert-type', 'info') }}";
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;

        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif

我使用ajax通过表单提交themes(如果该信息有用的话。)。这是themeCreate视图:

@extends('layouts.layout')

@section('title','Soumettre thématique')
@section('content')
<body>

<div class="container_fluid">
<div class="row">
<div class="alert alert-danger print-error-msg" style="display: none;">
<ul></ul>
</div>
</div>
</div>
    <form method="POST" action=" {{route('themes.store')}} ">
        @csrf
        <!-- Intitulé du thème -->
        <input type="text" name="intitule" id="intitule" placeholder="Intitulé du thème" required><br>
        <!-- Catégorie -->
        <select name="categorie" required>
            <option value="">-- Catégorie --</option>
            <option value="web">Développement web</option>
            <option value="appMobile">Programmation application mobile</option>
            <option value="secure">Sécurisation</option>
            <option value="other">Autre</option>
        </select> <br>
        <!-- Filière désirée -->
        <input type="checkbox" name="filiere[]" id="GL" value="GL" required>
        <label for="GL">Génie Logiciel</label><br>
        <input type="checkbox" name="filiere[]" id="SI" value="SI" required>
        <label for="SI">Sécurité Informatique</label><br>
        <input type="checkbox" name="filiere[]" id="IM" value="IM" required>
        <label for="IM">Internet et Multimédia</label><br>
        <input type="checkbox" name="filiere[]" id="SIRI" value="SIRI" required>
        <label for="SIRI">Systèmes d'Information et Réseaux Informatiques</label><br>
        <!-- Descriptif -->
        <textarea name="description" id="description" placeholder="Description de la thématique" required> {{old('description')}} </textarea><br>

        <input type="submit" name="submit" id="submit" value="Ajouter">
        <span id="error-message" class="text-danger"></span>
        <span id="success-message" class="text-success"></span>
    </form>

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(function (){
            var itsChecked = null;
            $('input[type=checkbox]').on('click', function(){
                if($('input[type=checkbox]:checked').length > 0){ //S'il y a au moins 1 ([...].length > 0) ckecked
                // alert('At least one is checked');
                    $('#GL').removeAttr("required");
                    $('#SI').removeAttr("required");
                    $('#IM').removeAttr("required");
                    $('#SIRI').removeAttr("required");
                }
                else if(!$('input[type=checkbox]:checked').length > 0){ //S'il n'y a aucun checked (!(at least 1)>0)
                // alert('None is checked');
                    $('#GL').attr('required','');
                    $('#SI').attr('required','');
                    $('#IM').attr('required','');
                    $('#SIRI').attr('required','');

            }
            });

            $('#submit').on('click',function(e){
                e.preventDefault();

                var _token = $("input[name='_token']").val();
                var intitule = $("input[name='intitule']").val();
                var categorie = $("select[name='categorie']").val();
                var filiereChecked = [];
                $.each($("input[type='checkbox']:checked"), function(){            
                filiereChecked.push($(this).val());
                });
                var filiere = filiereChecked.join(", ");
                var filiere = filiere.toString();
                var description = $("#description").val();

                $.ajax({
                    url: "{{route('themes.store')}}",
                    type: 'POST',
                    data: {
                        _token:_token,
                        intitule:intitule,
                        categorie:categorie,
                        filiere:filiere,
                        description:description
                    },
                    success: function(data){
                        if($.isEmptyObject(data.error)){
                            $('.print-error-msg').css('display','none');
                            toastr.success("{{ Session::get('message') }}");
                        }
                        else{
                            printErrorMsg(data.error);
                        }
                    }
                });
            });


        function printErrorMsg (msg) {

            $(".print-error-msg").find("ul").html('');

            $(".print-error-msg").css('display','block');

            $.each( msg, function( key, value ) {

                $(".print-error-msg").find("ul").append('<li>'+value+'</li>');

            });

        }
        });
    </script>
</body>
@endsection

这是负责在视图中显示敬酒消息的部分

success: function(data){
if($.isEmptyObject(data.error)){
    $('.print-error-msg').css('display','none');
    toastr.success("{{ Session::get('message') }}");
    }
    else{
      printErrorMsg(data.error);
    }
}

这是我的ThemeController store函数:

/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'intitule' => 'required|unique:themes,intitule',
            'categorie' => 'required:themes,categorie',
            'filiere' => 'required:themes,filiereDesiree',
            'description' => 'required:themes,description'
        ]);

        if ($validator->fails()) {

        return response()->json(['error'=>$validator->errors()->all()]);

        }

        $notification = array(
            'message' => 'Post created successfully!',
            'alert-type' => 'success'
        );

        $theme = new Theme;
        $theme->intitule = $request->input('intitule');
        $theme->description = $request->input('description');
        $theme->categorie = $request->input('categorie');
        $request->merge([
            'filiere' => implode(',', (array) $request->get('filiere'))
        ]);
        $theme->filiereDesiree = $request->input('filiere');
        $theme->save();

        return redirect('/themes/create')->with($notification);
    }

这里的问题是我得到了烤面包机(警报成功类型),但没有这样的消息:Toastr without message

我也认为toastr.success("{{ Session::get('$notification') }}");也是尝试过的,但没有任何改变。对此有任何想法吗??

0 个答案:

没有答案