|
| 1 | +# Card View |
| 2 | + |
| 3 | +An opt-in alternative to the default table row layout that displays results as a grid of cards. |
| 4 | + |
| 5 | +## Basic Setup |
| 6 | + |
| 7 | +Override `cardLayout()` in any `TableBuilder` or `QueryBuilder` component to enable the card view toggle: |
| 8 | + |
| 9 | +```php |
| 10 | +use ACTTraining\QueryBuilder\Support\CardLayout; |
| 11 | + |
| 12 | +class CoursesTable extends TableBuilder |
| 13 | +{ |
| 14 | + public function cardLayout(): ?CardLayout |
| 15 | + { |
| 16 | + return CardLayout::make() |
| 17 | + ->columns(3) |
| 18 | + ->view('courses.card'); |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +This adds the grid/list toggle buttons to the toolbar. Without overriding `cardLayout()`, nothing changes — the toggle is hidden by default. |
| 24 | + |
| 25 | +## Card Blade View |
| 26 | + |
| 27 | +Create the Blade partial referenced in `->view()`. It receives `$row` (the Eloquent model) and `$imageUrl` (resolved image URL or null): |
| 28 | + |
| 29 | +```blade |
| 30 | +{{-- resources/views/courses/card.blade.php --}} |
| 31 | +<div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden"> |
| 32 | + @if($imageUrl) |
| 33 | + <div class="aspect-video overflow-hidden"> |
| 34 | + <img src="{{ $imageUrl }}" alt="" class="w-full h-full object-cover" /> |
| 35 | + </div> |
| 36 | + @endif |
| 37 | +
|
| 38 | + <div class="p-4"> |
| 39 | + <p class="text-sm text-orange-500 font-medium">{{ $row->category->name }}</p> |
| 40 | + <h3 class="text-lg font-bold text-gray-900 mt-1">{{ $row->title }}</h3> |
| 41 | +
|
| 42 | + <div class="mt-3 space-y-1 text-sm text-gray-600"> |
| 43 | + <p>{{ $row->duration }} hours</p> |
| 44 | + <p>{{ $row->location }}</p> |
| 45 | + <p>{{ $row->delivery_method }}</p> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | +</div> |
| 49 | +``` |
| 50 | + |
| 51 | +## CardLayout Options |
| 52 | + |
| 53 | +| Method | Description | Default | |
| 54 | +|--------|-------------|---------| |
| 55 | +| `->columns(int)` | Number of grid columns at `lg` breakpoint | `3` | |
| 56 | +| `->image(string)` | Dot-notation key for the image URL on the model (e.g. `'photo_url'` or `'media.url'`) | `null` (no image) | |
| 57 | +| `->placeholder(string)` | Fallback image URL when the model's image is null | `null` | |
| 58 | +| `->view(string)` | Blade view to render each card | `'query-builder::card'` (basic default) | |
| 59 | + |
| 60 | +## Examples |
| 61 | + |
| 62 | +### Cards with images and a placeholder |
| 63 | + |
| 64 | +```php |
| 65 | +public function cardLayout(): ?CardLayout |
| 66 | +{ |
| 67 | + return CardLayout::make() |
| 68 | + ->image('featured_image_url') |
| 69 | + ->placeholder('/img/placeholder.jpg') |
| 70 | + ->columns(3) |
| 71 | + ->view('courses.card'); |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Cards without images (text-only) |
| 76 | + |
| 77 | +```php |
| 78 | +public function cardLayout(): ?CardLayout |
| 79 | +{ |
| 80 | + return CardLayout::make() |
| 81 | + ->columns(4) |
| 82 | + ->view('contacts.card'); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Using the default card template |
| 87 | + |
| 88 | +No custom view needed — renders image + model ID: |
| 89 | + |
| 90 | +```php |
| 91 | +public function cardLayout(): ?CardLayout |
| 92 | +{ |
| 93 | + return CardLayout::make() |
| 94 | + ->image('avatar_url'); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## What still works in card mode |
| 99 | + |
| 100 | +- **Pagination** — unchanged |
| 101 | +- **Search** — unchanged |
| 102 | +- **Filters / criteria** — unchanged |
| 103 | +- **Sorting** — applied at the query level, so cards render in sorted order |
| 104 | +- **Row click** — if `isClickable()` is true, clicking a card triggers the same action as clicking a table row |
0 commit comments