sangamkatwal / sangam-toastr
A simple Laravel package for Toastr notifications with support for session flash messages and AJAX responses. Handles success, error, warning, and info notifications seamlessly.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Language:Blade
Requires
- php: >=8.0
- laravel/framework: >=8.0
This package is auto-updated.
Last update: 2025-09-17 15:09:36 UTC
README
A simple and elegant Laravel package for Toastr notifications that works seamlessly with both session flash messages and AJAX responses.
Note: This is my first Laravel package! π If anything isn't clear, feel free to reach out via email or create an issue.
β¨ Features
- π Dual Support: Works with both session redirects and AJAX calls
- π¨ 4 Notification Types: Success, Error, Warning, Info
- π Zero Configuration: Works out of the box
- π± Responsive: Mobile-friendly notifications
- π§ Laravel Integration: Auto-discovery support
- π‘ Simple API: Consistent key-value pair format
π¦ Installation
composer require sangamkatwal/sangam-toastr
π§ Setup
1. Publish Assets
# Publish everything php artisan vendor:publish --tag=sangam-toastr # Or publish separately php artisan vendor:publish --tag=sangam-toastr-assets php artisan vendor:publish --tag=sangam-toastr-views
2. Include Assets in Layout
Add to your layouts/app.blade.php
(inside <head>
):
{{-- jQuery (required) --}} <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> {{-- Toastr Assets --}} <link rel="stylesheet" href="{{ asset('vendor/sangam-toastr/toastr.min.css') }}"> <script src="{{ asset('vendor/sangam-toastr/toastr.min.js') }}"></script>
3. Include Notifications
Add before closing </body>
tag in your layout:
@include('sangam-toastr::toastr')
π Usage
Session Flash Messages (Redirects)
// Success notification return redirect()->back()->with([ 'success' => true, 'message' => 'Data saved successfully!' ]); // Error notification return redirect()->back()->with([ 'error' => true, 'message' => 'Something went wrong!' ]); // Multiple messages (arrays supported) return redirect()->back()->with([ 'error' => true, 'message' => ['Field is required', 'Email must be valid'] ]);
AJAX Responses
// Success response return response()->json([ 'success' => true, 'message' => 'Operation completed!' ]); // Error response return response()->json([ 'error' => true, 'message' => 'Validation failed!' ]);
Frontend AJAX Implementation
$.ajax({ url: '/your-endpoint', type: 'POST', data: formData, success: function(response) { // This will automatically show toastr notifications ajax_response(response); // Alternative function name // showToastr(response); }, error: function(xhr) { // Handle server errors ajax_response({ error: true, message: 'Server error occurred' }); } });
Mixed Request Types (AJAX + Regular)
Perfect for controllers that handle both AJAX and regular requests:
public function store(Request $request) { try { // Your logic here... $message = 'Record created successfully!'; return $request->ajax() ? response()->json(['success' => true, 'message' => $message]) : redirect()->back()->with(['success' => true, 'message' => $message]); } catch (Exception $e) { $message = 'Something went wrong!'; return $request->ajax() ? response()->json(['error' => true, 'message' => $message]) : redirect()->back()->with(['error' => true, 'message' => $message]); } }
π Notification Types
Type | Usage |
---|---|
success |
β Success operations |
error |
β Error messages |
warning |
β οΈ Warning alerts |
info |
βΉοΈ Information notices |
βοΈ Requirements
- PHP: >= 8.0
- Laravel: 8.x, 9.x, 10.x, 11.x
- jQuery: 3.7.1 (recommended, not tested with other versions)
π Validation Examples
See ValidationExamples.md for detailed examples with Laravel validation.
π€ Contributing
This is my first package, so contributions and suggestions are very welcome!
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
π License
This package is open-sourced under the MIT license.
π¨βπ» Author
Sangam Katwal - katwalsangam@gmail.com
βοΈ Note By Author
This README was written with the help of AI based on my inputs. If you notice anything unclear or incorrect, feel free to reach out to me via email.
The Toastr CSS and JS used in this package are from the original Toastr.js v2.1.3. This package is completely free and open-source.
The main reason I built this package is because I often had to dig through old projects just to copy my custom Toastr response logic. By turning it into a reusable package, Iβve made it easier for myselfβand Iβm sharing it publicly in case it simplifies things for others too.
π¨βπ» Note For Developers
While this package encourages a simple and consistent response format (one notification type + one message), it does not restrict Laravelβs default flexibility. You can still attach additional values to your responses, whether for session flashes or AJAX.
For example:
// Redirect response with extra data return redirect()->back()->with([ 'success' => true, 'message' => 'Data saved successfully!', 'status' => 200, 'extra' => 'Any other value' ]); // AJAX response with extra fields return response()->json([ 'success' => true, 'message' => 'Data saved successfully!', 'status' => 200, 'data' => ['id' => 123, 'name' => 'Sample'] ]); So, you still get all the power of Laravel responses while enjoying clean, consistent Toastr notifications. π --- β **Star this repo if it helped you!**