Laravel Views
repository·master·Indexed 20 days ago
https://github.com/gustavinho/laravel-viewsA package for building common UI components, such as data tables, using the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire). It includes Artisan commands for scaffolding views, filters, and actions, as well as tools for managing query string search, filtering, and sorting.
What's inside laravel-views
- Laravel Views is a package designed to create beautiful, common UI views (such as data tables) using the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire).
Upgrade from 2.4.0 to 2.4.1
masterIf you have previously published Blade components for customization, you must re-publish them to ensure you have the latest internal component structures. Run the following command to update your published components:
php artisan vendor:publish --tag=views --provider='LaravelViews\LaravelViewsServiceProvider'Upgrade from 2.2 to 2.3
masterUpgrading from version 2.2 to 2.3 requires several steps due to changes in Blade directives, assets, and component structures:
Clear Cached Views: Because Blade directives have changed, clear your view cache:
php artisan view:clearUpdate Public Assets: The main JS and CSS files have changed. Re-publish them using the
--forceflag:php artisan vendor:publish --tag=public --provider='LaravelViews\LaravelViewsServiceProvider' --forceUpdate Blade Components: If you have customized published components, re-publish them to get the latest versions:
php artisan vendor:publish --tag=views --provider='LaravelViews\LaravelViewsServiceProvider'Update
renderIf()in Action Classes: TherenderIf()method now requires a newViewparameter. Update your action classes as follows:use LaravelViews//... (see example)Config File (Optional): New variants have been added. You can re-publish the config to customize them, though the package will use defaults if you don't.
Refactor
repository()method (Optional): If yourrepository()method simply returns a base query (e.g.,User::query()), you can now replace it with aprotected $modelproperty.
// Update renderIf() signature namespace App\Actions; use LaravelViews\Actions\Action; use LaravelViews\Views\View; // new line class YourAction extends Action { public function renderIf($item, View $view) // add the view parameter { // your content } } // Refactor repository() to $model /* Before */ public function repository(): Builder { return User::query(); } /** After */ protected $model = User::class;Core Data Contract Bindings
masterThe package uses Laravel's container to bind specific implementations to core data contracts. If you are implementing these interfaces, the following default implementations are bound:
LaravelViews\Data\Contracts\Searchable$\rightarrow$LaravelViews\Data\TableViewSearchDataLaravelViews\Data\Contracts\Filterable$\rightarrow$LaravelViews\Data\TableViewFilterDataLaravelViews\Data\Contracts\Sortable$\rightarrow$LaravelViews\Data\TableViewSortData
Publish Laravel Views assets and configuration
masterTo customize the package's assets, views, or configuration, use the Laravel
vendor:publishcommand. The package provides three distinct publishable tags:public: Publishes JavaScript, CSS, and Tailwind assets to yourpublic/vendor/laravel-views.jsdirectory.config: Publishes the package configuration file toconfig/laravel-views.php.views: Publishes component and view templates to yourresources/views/vendor/laravel-views/directory, allowing you to override the default UI.
# Publish configuration php artisan vendor:publish --tag=config # Publish public assets (JS/CSS) php artisan vendor:publish --tag=public # Publish view templates for customization php artisan vendor:publish --tag=viewsSelect UI component variants using the Variants class
masterThe
LaravelViews\UI\Variantsclass allows you to fluently select a UI component and a specific visual variant. This selection is used to resolve configuration values (like CSS classes) from thelaravel-viewsconfiguration file.Available component methods include:
button($variant): Selects thebuttonscomponent.alert($variant): Selects thealertscomponent.paginator($variant): Selects thepaginatorcomponent.badge($variant): Selects thebadgescomponent.img($variant): Selects theimagescomponent.featherIcon($variant): Selects theiconscomponent.
Each method accepts an optional
$variantstring.use LaravelViews\\UI\Variants; // Example: Selecting a success alert variant $variants = (new Variants())->alert('success');Laravel Views Blade component prefixes
masterThe package registers several Blade components using the
lv-prefix.Anonymous Components: Components are registered with the pattern
lv-{component-name}. These are mapped from the package's internal component list.Class-based Components:
lv-layout: The main layout component.lv-dynamic-component: A dynamic component handler (DynamicComponent).
Configure table column headers with the Header class
masterThe
LaravelViews\UI\Headerclass is used to define the properties of a column header in a table view. You can specify the display title, the field used for sorting, and the column width.To start configuring a header, use the
title()method, which initializes a new instance. Subsequent configuration methods likesortBy()andwidth()can be chained to the instance.use LaravelViews\UI\Header; $header = Header::title('User Name') ->sortBy('name') ->width('200px');Retrieve and cast boolean filter values
masterThe
getFilterValuesmethod retrieves thefiltersparameter from the query string. It specifically handles boolean filters by iterating through the filter array and casting string values (e.g.,'true'or'false') into actual boolean types usingfilter_varwithFILTER_VALIDATE_BOOLEAN. If no filters are found, it returns the provided$currentValue.// Assuming $queryStringData is an instance of QueryStringData $filters = $queryStringData->getFilterValues($currentValue);Get arbitrary query string values
masterThe
getValuemethod allows you to retrieve any specific field from the query string. It takes the field name and a default value to return if the field is missing from the request.// Assuming $queryStringData is an instance of QueryStringData $value = $queryStringData->getValue('my_param', 'default_value');Use Laravel Views Blade directives
masterThe package provides two Blade directives to easily include the necessary scripts and styles in your application layouts:
@laravelViewsScripts($options): Renders the required JavaScript.@laravelViewsStyles($options): Renders the required CSS.
@laravelViewsStyles @laravelViewsScriptsGet the title and icon for alert variants
masterThe
Variantsclass provides helper methods to retrieve human-readable titles and Feather icon names specifically for thealertcomponent variants.Alert Variants Mapping:
Variant title()icon()successSuccess checkdangerError xwarningWarning alert-circleNote: These methods only work if an
alertcomponent has been selected via thealert()method.$variants = (new Variants())->alert('success'); echo $variants->title(); // Outputs: Success echo $variants->icon(); // Outputs: check