- Using
laravel-admin
for backend development, we added a notification bell in the top-right corner
- As shown above, clicking the bell icon should clear notifications and refresh the current page. Implemented using
laravel-admin
’saction
:
<?php
namespace App\Admin\Actions\Custom;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
class MarkNotificationRead extends Action
{
protected $selector = '.mark-notification-read';
public function handle(Request $request)
{
// forget notifications
return $this->response()->success("Marked all as read")->refresh();
}
public function html()
{
// get notifications
$count = 1;
$countText = "<span class=\"label label-danger\">{$count}</span>";
return "<li title='Click to mark as read'>
<a href=\"#\" class='mark-notification-read'>
<i data-count='{$count}' id='notification' class=\"fa fa-bell fa-rotate-0\"></i>
{$countText}
</a>
</li>";
}
}
- The pseudo-code above implements the notification feature. However,
laravel-admin
’s PJAX implementation only refreshes content areas, leaving the bell badge unchanged.
Attempted Solutions:
- Tried disabling PJAX using
\Admin::disablePjax();
in handler - ineffective (should be applied at page level) - Overrode the
handleActionPromise
method in the Action class:
public function handleActionPromise()
{
$resolve = <<<'SCRIPT'
var actionResolver = function (data) {
// ... [original code]
if (then.action == 'refresh') {
// Added this critical line
window.location.reload();
return;
}
// ... [remaining code]
SCRIPT;
\Encore\Admin\Admin::script($resolve);
return <<<'SCRIPT'
process.then(actionResolver).catch(actionCatcher);
SCRIPT;
}
Comparison with Dcat Admin:
Notably, Dcat Admin provides better extensibility through overridable methods:
protected function resolverScript()
{
return <<<'JS'
function (target, results) {
// Custom refresh handling
window.location.reload();
}
JS;
}
Key Takeaways
- Avoid getting tunnel-visioned by perceived constraints (PJAX limitations in this case)
- When stuck, consider re-examining parent class capabilities before complex workarounds
- Modern admin frameworks often provide extension points - explore inheritance hierarchies before reinventing wheels
This experience demonstrates how stepping back from conventional thinking patterns can reveal simpler solutions hidden in framework mechanics.