Widgets are cards that hold some information or any content you want to display.
You can generate new action classes by calling the root:widget artisan command:
php artisan root:widget PostCount
You can register actions in resources and extracts, by using the widgets method.
use App\Root\Widgets\PostCount;
use Illuminate\Http\Request;
/**
* Define the widgets for the resource.
*/
public function widgets(Request $request): array
{
return array_merge(parent::widgets($request), [
PostCount::make(),
]);
}
You may allow or disallow interaction with widgets. To do so, you can call the authorize method on the widget instance:
$widget->authorize(static function (Request $request): bool {
return $request->user()->can('viewPostCount');
});
You may show or hide widgets based on the current resource view. For example, some widgets might be visible on the index page, while others should be hidden. You can easily customize the action visibility logic using the visibleOn and hiddenOn methods:
$field->visibleOn(['index']);
$field->hiddenOn(['index']);
A widget class must have a valid template property, that holds a real blade template:
use Cone\Root\Widgets\Widget;
class PostCount extends Widget
{
/**
* The Blade template.
*/
protected string $template = 'widgets.post-count;
}
You can customize the data passed to the blade template by using the data method:
use App\Models\Post;
use Cone\Root\Widgets\Widget;
use Illuminate\Http\Request;
class PostCount extends Widget
{
/**
* Get the data.
*/
public function data(Request $request): array
{
return array_merge(parent::data($request), [
'count' => Post::query()->count(),
]);
}
}