Installation:
composer require alex198710/pnotify
Publish assets (if using Laravel Mix/Vite):
php artisan vendor:publish --tag=pnotify-assets
Add to resources/js/app.js (or equivalent):
import 'vendor/alex198710/pnotify/assets/js/pnotify.custom.min.js';
import 'vendor/alex198710/pnotify/assets/css/pnotify.custom.min.css';
First Use Case: Trigger a notification in a Blade view:
<script>
$(function() {
new PNotify({
title: 'Success!',
text: 'Your action was completed.',
type: 'success'
});
});
</script>
Where to Look First:
vendor/alex198710/pnotify/assets/ for raw files.Reusable Notification Service:
Create a helper class in app/Services/NotificationService.php:
class NotificationService {
public static function success($title, $text) {
return "<script>new PNotify({ title: '{$title}', text: '{$text}', type: 'success' });</script>";
}
}
Usage in Blade:
{!! NotificationService::success('Success', 'Data saved!') !!}
Laravel Events + Notifications:
Bind to events in EventServiceProvider:
public function boot() {
OrderCreated::listen(function ($order) {
return "<script>new PNotify({ title: 'Order Created', text: '#{$order->id}' });</script>";
});
}
Dynamic Notifications with Blade:
@if(session('success'))
<script>
new PNotify({
title: 'Success',
text: '{{ session('success') }}',
type: 'success'
});
</script>
@endif
AJAX Responses: Return notifications in API responses:
$.ajax({
url: '/update',
success: function(response) {
if (response.notification) {
new PNotify(response.notification);
}
}
});
Asset Loading:
jQuery Dependency: PNotify requires jQuery. If using Laravel Mix, ensure jQuery is loaded first:
import 'jquery';
import 'pnotify.custom.min.js';
Z-Index Conflicts: PNotify notifications may appear behind modals. Override styles:
.pnotify {
z-index: 9999 !important;
}
Symfony Legacy Config:
The package’s Config.yml is for Symfony. For Laravel, use Laravel Mix/Vite or manual asset publishing.
PNotify is not defined → Verify jQuery and PNotify JS are loaded.Custom Themes:
Override the default CSS in resources/css/app.css:
.pnotify {
background: #2d3748;
color: white;
}
Laravel Facade: Create a facade for cleaner usage:
// app/Facades/PNotify.php
class PNotify extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() { return 'pnotify'; }
}
Bind in AppServiceProvider:
public function boot() {
$this->app->singleton('pnotify', function() {
return new NotificationService();
});
}
Queue Notifications: Store notifications in the session and render them after page load:
session(['notification' => ['type' => 'success', 'text' => 'Processed!']]);
Blade:
@if(session('notification'))
<script>
$(document).ready(function() {
new PNotify({{ json_encode(session('notification')) }});
});
</script>
@endif
How can I help you explore Laravel packages today?