简体   繁体   中英

Toastr message is not working in laravel-8

I just added a toast on form submit but it didn't work and I don't understand the problem.

public function create(Request $data)
{

    $data->validate([
        'name'          =>  'required|string|max:255',
        'phone'         =>  'required|unique:users|numeric|digits:11',
        'cnic'          =>  'nullable|unique:users|numeric|digits:13',
        'email'         =>  'required|string|email|max:255|unique:users',
        'designation'   =>  'nullable|string',
        'department'    =>  'nullable|string',
        'password'      =>  'required|string|min:4',
    ]);

    User::create([
        'name'          => $data['name'],
        'phone'         => $data['phone'],
        'cnic'          => $data['cnic'],
        'email'         => $data['email'],
        'role'          => $data['role'],
        'designation'   => $data['designation'],
        'department'    => $data['department'],
        'password'      => Hash::make($data['password']),
    ]);

    return redirect()->route('manage-users')->with('message', 'User created successfully!');
}

This is my controller and I use that way in my blade

@if(Session::has('message'))
    toastr.info("{{ Session::get('message') }}");
@endif

and this is the css:

@section('vendor-style')
{{-- vendor css files --}}
<link rel="stylesheet" href="{{ asset('vendors/css/extensions/toastr.css') }}">
@endsection

@section('page-style')
{{-- Page Css files --}}
<link rel="stylesheet" href="{{ asset('css/plugins/extensions/toastr.css') }}">
@endsection

and js files:

@section('vendor-script')
{{-- vendor files --}}
<script src="{{ asset('vendors/js/extensions/toastr.min.js') }}"></script>
@endsection

@section('page-script')
{{-- Page js files --}}
<script src="{{ asset('js/scripts/extensions/toastr.js') }}"></script>
@endsection

and these files are working fine. Thank you for your help!

as it is a javascript library you need to put this code inside <script> tag

@if(Session::has('message'))
    toastr.info("{{ Session::get('message') }}");
@endif

to

@if(Session::has('message'))
    <script>
        toastr.info("{{ Session::get('message') }}");
    </script>
@endif

if still not working means it is not trigger your code so you can do like this

@if(Session::has('message'))
<script>
    $(function(){
            toastr.info("{{ Session::get('message') }}");
        })
</script>
@endif

this code will trigger this code once page is loaded

I created a package for the tall stack (Laravel 8 and will keep it updated). Feel free to check it out:

https://github.com/usernotnull/tall-toasts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM