Laravel Views

repository·master·Indexed 20 days ago

https://github.com/gustavinho/laravel-views

A 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.

Tokens
3.9K
Snippets
21
Records
25
Agent score
70%

What's inside laravel-views

  1. Upgrade from 2.4.0 to 2.4.1

    master

    If 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'
  2. Upgrade from 2.2 to 2.3

    master

    Upgrading from version 2.2 to 2.3 requires several steps due to changes in Blade directives, assets, and component structures:

    1. Clear Cached Views: Because Blade directives have changed, clear your view cache:

      php artisan view:clear
    2. Update Public Assets: The main JS and CSS files have changed. Re-publish them using the --force flag:

      php artisan vendor:publish --tag=public --provider='LaravelViews\LaravelViewsServiceProvider' --force
    3. Update 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'
    4. Update renderIf() in Action Classes: The renderIf() method now requires a new View parameter. Update your action classes as follows:

      use LaravelViews//... (see example)
    5. 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.

    6. Refactor repository() method (Optional): If your repository() method simply returns a base query (e.g., User::query()), you can now replace it with a protected $model property.

    // 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;
  3. Core Data Contract Bindings

    master

    The 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\TableViewSearchData
    • LaravelViews\Data\Contracts\Filterable $\rightarrow$ LaravelViews\Data\TableViewFilterData
    • LaravelViews\Data\Contracts\Sortable $\rightarrow$ LaravelViews\Data\TableViewSortData
  4. Publish Laravel Views assets and configuration

    master

    To customize the package's assets, views, or configuration, use the Laravel vendor:publish command. The package provides three distinct publishable tags:

    1. public: Publishes JavaScript, CSS, and Tailwind assets to your public/vendor/laravel-views.js directory.
    2. config: Publishes the package configuration file to config/laravel-views.php.
    3. views: Publishes component and view templates to your resources/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=views
  5. Select UI component variants using the Variants class

    master

    The LaravelViews\UI\Variants class 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 the laravel-views configuration file.

    Available component methods include:

    • button($variant): Selects the buttons component.
    • alert($variant): Selects the alerts component.
    • paginator($variant): Selects the paginator component.
    • badge($variant): Selects the badges component.
    • img($variant): Selects the images component.
    • featherIcon($variant): Selects the icons component.

    Each method accepts an optional $variant string.

    use LaravelViews\\UI\Variants;
    
    // Example: Selecting a success alert variant
    $variants = (new Variants())->alert('success');
  6. Laravel Views Blade component prefixes

    master

    The 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).
  7. Configure table column headers with the Header class

    master

    The LaravelViews\UI\Header class 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 like sortBy() and width() can be chained to the instance.

    use LaravelViews\UI\Header;
    
    $header = Header::title('User Name')
        ->sortBy('name')
        ->width('200px');
  8. Retrieve and cast boolean filter values

    master

    The getFilterValues method retrieves the filters parameter 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 using filter_var with FILTER_VALIDATE_BOOLEAN. If no filters are found, it returns the provided $currentValue.

    // Assuming $queryStringData is an instance of QueryStringData
    $filters = $queryStringData->getFilterValues($currentValue);
  9. Get arbitrary query string values

    master

    The getValue method 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');
  10. Get the title and icon for alert variants

    master

    The Variants class provides helper methods to retrieve human-readable titles and Feather icon names specifically for the alert component variants.

    Alert Variants Mapping:

    Varianttitle()icon()
    successSuccesscheck
    dangerErrorx
    warningWarningalert-circle

    Note: These methods only work if an alert component has been selected via the alert() method.

    $variants = (new Variants())->alert('success');
    
    echo $variants->title(); // Outputs: Success
    echo $variants->icon();  // Outputs: check