Toastr message is not working in laravel-8

Viewed 3040

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!

2 Answers

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

Related