### Install Laravel Views Package
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Install the package via Composer and publish its assets and configuration files.
```bash
composer require laravel-views/laravel-views
# Publish public assets (JS, CSS)
php artisan vendor:publish --tag=public --provider='LaravelViews\LaravelViewsServiceProvider'
# Optionally publish the config file
php artisan vendor:publish --tag=config --provider='LaravelViews\LaravelViewsServiceProvider'
# Optionally publish Blade views for customization
php artisan vendor:publish --tag=views --provider='LaravelViews\LaravelViewsServiceProvider'
```
--------------------------------
### Artisan Scaffold Commands for Views
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Generate boilerplate class files for different types of views, filters, and actions using Artisan commands.
```bash
# Create a new TableView class
php artisan make:table-view UsersTableView
# Create a new GridView class
php artisan make:grid-view ProductsGridView
# Create a new ListView class
php artisan make:list-view OrdersListView
# Create a filter class
php artisan make:filter ActiveUsersFilter
# Create an action class
php artisan make:action DeleteUserAction
# Generic make command (prompts for type)
php artisan make:view MyView
```
--------------------------------
### Implement Products GridView
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Extend GridView to display products as responsive cards. Implement the card() method to define data passed to each card. Configure search fields, maximum columns, background styling, and sortable options.
```php
'Name',
'price' => 'Price',
'created_at' => 'Newest',
];
}
/** Data passed to each card component */
public function card($model): array
{
return [
'title' => $model->name,
'image' => $model->image_url,
'subtitle' => '$' . number_format($model->price, 2),
];
}
/** Navigate to a detail page when a card is clicked */
public function onCardClick($model)
{
return redirect()->route('products.show', $model);
}
protected function filters(): array
{
return [new CategoryFilter];
}
}
```
--------------------------------
### Use RedirectAction for Navigation
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Employ RedirectAction for straightforward navigation without defining a custom action class. Specify the route, link text, and an optional icon.
```php
use LaravelViews\Actions\RedirectAction;
protected function actionsByRow(): array
{
return [
new RedirectAction('users.edit', 'Edit', 'edit'),
new RedirectAction('users.show', 'View', 'eye'),
];
}
```
--------------------------------
### Implement Users TableView
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Extend TableView to create a searchable, filterable, and sortable data table for users. Implement headers() and row() methods for column definitions and data retrieval. Configure search fields, pagination, filters, and row actions.
```php
with('roles')->where('verified', true);
// }
// Fields to search across (supports dot-notation for relations)
public $searchBy = ['name', 'email', 'roles.name'];
// Items per page
protected $paginate = 15;
/** Column headers */
public function headers(): array
{
return [
Header::title('Name')->sortBy('name'),
Header::title('Email')->sortBy('email')->width('250px'),
Header::title('Status'),
Header::title('Created'),
];
}
/** Row data — called once per item */
public function row($model): array
{
return [
$model->name,
$model->email,
UI::badge($model->active ? 'Active' : 'Inactive', $model->active ? 'success' : 'danger'),
$model->created_at->format('Y-m-d'),
];
}
/** Filters shown in the toolbar */
protected function filters(): array
{
return [
new ActiveUsersFilter,
];
}
/** Per-row action buttons */
protected function actionsByRow(): array
{
return [
new EditUserAction,
new DeleteUserAction,
];
}
/** Bulk actions (appear when rows are selected) */
protected function bulkActions(): array
{
return [
new DeleteUserAction,
];
}
}
```
--------------------------------
### Configure Sortable Column Headers with Header Facade
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Use the Header facade to define column configurations for TableView, including simple titles, sortable columns by database fields, and fixed column widths.
```php
use LaravelViews\Facades\Header;
public function headers(): array
{
return [
// Simple non-sortable header
Header::title('Avatar'),
// Sortable by a database column
Header::title('Name')->sortBy('name'),
// Sortable with a fixed column width
Header::title('Email')->sortBy('email')->width('200px'),
// Fixed width only (not sortable)
Header::title('Actions')->width('120px'),
];
}
```
--------------------------------
### Create a DetailView Component
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Extend LaravelViews\Views\DetailView to display details of a single Eloquent model. Implement the detail() method to define the attributes to show. Supports custom headings and actions.
```php
name, $model->email];
}
/** Return an associative array for a default attributes list */
public function detail($model): array
{
return [
'Name' => $model->name,
'Email' => $model->email,
'Role' => $model->role,
'Registered' => $model->created_at->format('F j, Y'),
'Status' => $model->active ? 'Active' : 'Inactive',
];
}
public function actions(): array
{
return [
new EditUserAction,
new DeleteUserAction,
];
}
}
```
--------------------------------
### Render UI Components with UI Facade
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Utilize the UI facade for inline rendering of badges, avatars, links, icons, editable fields, attribute lists, and custom Blade components within Laravel Views.
```php
use LaravelViews\Facades\UI;
// Badge: UI::badge(string $title, string $type = 'default')
// Types: 'success', 'danger', 'warning', 'info', 'default'
UI::badge('Active', 'success');
UI::badge('Banned', 'danger');
// Avatar image (circular)
UI::avatar($user->avatar_url);
// Clickable link
UI::link('View profile', route('users.show', $user));
// Feather icon with optional type/class
UI::icon('check-circle', 'success');
UI::icon('alert-triangle', 'warning', 'ml-2');
// Inline editable field (inline editing)
UI::editable($model, 'name');
// Attributes list (for DetailView)
UI::attributes(['Name' => $user->name, 'Email' => $user->email], ['stripe' => true]);
// Custom Blade component
UI::component('components.my-badge', ['label' => 'Custom']);
```
--------------------------------
### Publish public assets
Source: https://github.com/gustavinho/laravel-views/blob/master/README.md
The main JavaScript and CSS files have changed in version 2.3. Re-publish the public assets to include the updated files.
```bash
php artisan vendor:publish --tag=public --provider='LaravelViews\LaravelViewsServiceProvider' --force
```
--------------------------------
### Create a ListView Component
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Extend LaravelViews\Views\ListView to render items as a vertical list. Implement the item() method to define the data for each list item. Supports search, filters, and sortable columns.
```php
'Date', 'total' => 'Total'];
}
/** Data passed to each list item component */
public function item($model): array
{
return [
'title' => 'Order #' . $model->reference,
'subtitle' => $model->customer_name,
'value' => UI::badge('$' . $model->total, 'info'),
];
}
protected function actionsByRow(): array
{
return [new ViewOrderAction];
}
}
```
--------------------------------
### Implement a Date Filter
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Create a date picker filter by extending LaravelViews\Filters\DateFilter. The apply() method receives a Carbon instance for date comparisons.
```php
whereDate('created_at', '>=', $value);
}
}
```
--------------------------------
### Publish Blade components
Source: https://github.com/gustavinho/laravel-views/blob/master/README.md
If you have previously published and customized internal Blade components, you need to publish them again after upgrading to ensure you have the latest versions.
```bash
php artisan vendor:publish --tag=views --provider='LaravelViews\LaravelViewsServiceProvider'
```
--------------------------------
### Render View with Layout
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Embed a view within an existing Blade layout using a named section. This is useful when not using Livewire's full-page components.
```php
use LaravelViews\Facades\LaravelViews;
// In a controller method
public function index()
{
$html = LaravelViews::create(new UsersTableView)
->layout('layouts.app', 'content', ['pageTitle' => 'Users'])
->render();
return response($html);
}
// Or pass the rendered HTML directly to a view
return view('admin.dashboard', [
'usersTable' => LaravelViews::create(new UsersTableView)->render(),
]);
```
--------------------------------
### Implement Custom Action in Laravel Views
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Extend LaravelViews\Actions\Action and implement the handle() method. Use $this->success() or $this->error() for feedback. The renderIf() method can conditionally display the action.
```php
id() !== $item->id;
}
/** $item is the Eloquent model for row actions, or an array of IDs for bulk actions */
public function handle($item, View $view = null)
{
try {
$item->update(['active' => !$item->active]);
$this->success('User status updated successfully.');
} catch (\Exception $e) {
$this->error('Could not update user status.');
}
}
}
```
--------------------------------
### Implement Confirmable Action for User Deletion
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Add the Confirmable trait to an action to prompt the user for confirmation before execution. Customize the confirmation message using getConfirmationMessage().
```php
name}?"
: 'Are you sure you want to delete the selected users?';
}
public function handle($item, $view = null)
{
is_array($item)
? \App\Models\User::destroy($item)
: $item->delete();
$this->success('Deleted successfully.');
}
}
```
--------------------------------
### Include ListView in Blade
Source: https://context7.com/gustavinho/laravel-views/llms.txt
Render the ListView component in your Blade views using the livewire directive.
```blade