Install & Publish Config
composer require rohitshakya/laravel-beacon
php artisan vendor:publish --tag=beacon-config
config/beacon.php for default settings (e.g., notifiable_model, channels, views).Add Notifiable Trait
Extend your model (e.g., User, Reseller) with Laravel’s Notifiable trait:
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable; // Required for Beacon
}
Trigger a Test Notification
use App\Notifications\TestNotification;
// In a controller or command
auth()->user()->notify(new TestNotification());
/notifications).Livewire Integration (Optional)
Add the Livewire component to your layout (e.g., resources/views/layouts/app.blade.php):
@livewire('beacon::topbar')
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use rohitshakya\Beacon\Messages\BeaconMessage;
class ProfileUpdated extends Notification
{
public function via($notifiable)
{
return ['database', 'beacon']; // Add 'beacon' to use Beacon
}
public function toBeacon($notifiable)
{
return (new BeaconMessage)
->title('Profile Updated')
->body('Your profile has been successfully updated.')
->icon('fas fa-user-edit');
}
}
auth()->user()->notify(new ProfileUpdated());
Multi-Notifiable Models
User, Reseller) by configuring notifiable_models in beacon.php:
'notifiable_models' => [
\App\Models\User::class,
\App\Models\Reseller::class,
],
beacon()->forModel($model) in Livewire components to target specific channels.Realtime Updates with Echo/Reverb
beacon.php:
'broadcasting' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => ['cluster' => env('PUSHER_APP_CLUSTER')],
],
Echo.channel(`beacon.${userId}`)
.listen('BeaconEvent', (data) => {
console.log('New notification:', data);
});
Customizing Views
php artisan vendor:publish --tag=beacon-views
resources/views/vendor/beacon/topbar.blade.php or inbox.blade.php to match your app’s design.Multiple Topbars on a Page
beacon helper to generate unique components:
@livewire('beacon::topbar', ['model' => \App\Models\Reseller::class])
beacon.php:
'channels' => [
'users' => 'beacon.users',
'resellers' => 'beacon.resellers',
],
Livewire Components
use rohitshakya\Beacon\Http\Livewire\Topbar;
class CustomTopbar extends Topbar
{
public function mount()
{
parent::mount();
// Add custom logic (e.g., filter notifications)
}
}
AppServiceProvider:
Livewire::component('custom-topbar', \App\Http\Livewire\CustomTopbar::class);
Database Notifications
notifications table exists (Laravel’s default migration):
php artisan migrate
Browser Events
window.addEventListener('beacon.notification.created', (e) => {
console.log('New notification:', e.detail);
});
Testing
$user = User::factory()->create();
$notification = new TestNotification();
Notification::fake();
$user->notify($notification);
Notification::assertSentTo($user, TestNotification::class);
Missing Notifiable Trait
Call to undefined method App\Models\User::route().use Illuminate\Notifications\Notifiable;.Broadcasting Not Configured
broadcasting settings in beacon.php and install Pusher/Reverb:
composer require pusher/pusher-php-server
Channel Mismatch
notifiable_models in beacon.php includes the correct model classes.Livewire Component Not Found
@livewire('beacon::topbar') fails with "Component not found".php artisan vendor:publish --tag=beacon-views) or register the component manually in AppServiceProvider.CSRF Token Mismatch
@csrf and Livewire’s @stack('scripts').Check Logs
.env:
APP_DEBUG=true
tail -f storage/logs/laravel.log | grep beacon
Verify Database
notifications table for pending notifications:
SELECT * FROM notifications WHERE read_at IS NULL;
Test Broadcasting
Echo.connector.debug = true;
Custom Notification Types
BeaconMessage for custom UI:
class CustomMessage extends BeaconMessage
{
public function __construct()
{
$this->icon = 'fas fa-bell';
$this->priority = 'high';
}
}
Conditional Topbar
@auth
@if(auth()->user()->isAdmin())
@livewire('beacon::topbar')
@endif
@endauth
Mark All as Read
// In a Livewire component
public function markAllAsRead()
{
auth()->user()->unreadNotifications()->update(['read_at' => now()]);
}
Localization
php artisan vendor:publish --tag=beacon-lang
resources/lang/en/beacon.php.Performance
// In a Livewire component
public $notifications = [];
public function loadMore()
{
$this->notifications = auth()->user()->notifications()->paginate(10);
}
Dark Mode Support
@if(config('beacon.dark_mode'))
<div class="bg-gray-800 text-white">
@livewire('beacon::topbar')
</div>
@else
<div class="bg-white">
@livewire('beacon::topbar')
</div>
@endif
Event Listeners
use Illuminate\Support\Facades\Event;
How can I help you explore Laravel packages today?