Skip to content

Commit c6b4ad4

Browse files
authored
Merge pull request #631 from Laravel-Backpack/chips
2 parents ff321f8 + 7ad8a04 commit c6b4ad4

File tree

3 files changed

+288
-1
lines changed

3 files changed

+288
-1
lines changed

7.x-dev/base-widgets.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ Shows a dataform component from a particular CrudController. For more info about
364364
]
365365
```
366366

367+
<hr>
368+
369+
<a name="chip"></a>
370+
### Chip
371+
372+
Shows a chip blade view - which is useful to show more information about a database entry, using little screen real estate.
373+
374+
```php
375+
[
376+
'type' => 'chip',
377+
'view' => 'crud::chips.general',
378+
'title' => 'invoices',
379+
'entry' => Invoice::first(),
380+
]
381+
```
382+
383+
367384
<hr>
368385

369386
<a name="datatable"></a>

7.x-dev/crud-chips.md

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
# Chips
2+
3+
---
4+
5+
<a name="about"></a>
6+
## About
7+
8+
A chips helps show the information of a database entry, in a format that takes up little space visually. It can be used anywhere you want, but it's particularly useful inside operations to:
9+
- show more info inside a table cell in the **ListOperation**;
10+
- show a related item in more detail in the **ShowOperation**;
11+
12+
A chip consists of only one file - a blade file with the same name as the chip type (ex: ```general.blade.php```). Backpack provides you with one chip type, that is very general (hence the name). This chip is designed to accomodate most types of database entries - but if your needs are more particular, you can easily [create an entirely new chip type](#creating-a-custom-chip-type).
13+
14+
<a name="default-chip-types"></a>
15+
### Default Chip Types
16+
17+
<a name="general-chip"></a>
18+
#### General Chip
19+
20+
![Backpack v7 general chip](https://backpackforlaravel.com/uploads/v7/general_chip.jpg)
21+
22+
This chip was designed to be so general, that it's useful to show _most_ types of entries from the database. The general chip has 3 sections: `heading`, `image` and `details`. All sections are optional. Each of those sections has one mandatory attribute, `content`. Any other attributes you specify on those sections will be placed on that DOM element.
23+
24+
Here's a very minimal usage of the general chip:
25+
26+
```php
27+
@include('crud::chips.general', [
28+
'heading' => [
29+
'content' => 'John Doe',
30+
],
31+
'image' => [
32+
'content' => asset('uploads/person1.jpg'),
33+
],
34+
'details' => [
35+
[
36+
'icon' => 'la la-hashtag',
37+
'content' => '8AH13A7',
38+
],
39+
[
40+
'icon' => 'la la-envelope',
41+
'content' => 'john.doe@example.com',
42+
],
43+
[
44+
'icon' => 'la la-phone',
45+
'content' => '+1 (555) 123-4567',
46+
]
47+
]
48+
])
49+
```
50+
51+
But you can also specify more attributes, to enhance your chip with links, titles etc:
52+
53+
```php
54+
@include('crud::chips.general', [
55+
'heading' => [
56+
'content' => 'John Doe',
57+
'href' => 'https://google.com',
58+
'target' => '_blank',
59+
'title' => 'Example of a chip without URL',
60+
],
61+
'image' => [
62+
'content' => asset('uploads/person1.jpg'),
63+
'element' => 'a',
64+
'href' => 'https://chatgpt.com',
65+
'target' => '_blank',
66+
'title' => 'Image can have its own URL, but why?! Falls back to the one in the heading',
67+
],
68+
'details' => [
69+
[
70+
'icon' => 'la la-hashtag',
71+
'content' => '8AH13A7',
72+
'url' => 'mailto:john.doe@example.com',
73+
'title' => 'Click to email',
74+
],
75+
[
76+
'icon' => 'la la-envelope',
77+
'content' => 'john.doe@example.com',
78+
'url' => 'mailto:john.doe@example.com',
79+
'title' => 'Click to email',
80+
],
81+
[
82+
'icon' => 'la la-phone',
83+
'content' => '+1 (555) 123-4567',
84+
'url' => 'tel:+15551234567',
85+
'title' => 'Click to call',
86+
]
87+
]
88+
])
89+
```
90+
91+
<a name="how-to-use-chips"></a>
92+
### How to use chips
93+
94+
Depending on _where_ you want to use a chip, there are a few ways you can do that. Remember - a chip is a simple blade file, so the methods below should be pretty intuitive:
95+
96+
<a name="how-to-use-a-chip-inside-a-custom-blade-view"></a>
97+
#### How to use a chip inside a custom blade view
98+
99+
If you want to load a chip inside a custom page, custom component or anything else custom, you can just include the blade view directly, and pass whatever attributes you want to show. For example, if you want to use the `general` chip you can just include that blade file, and pass some of the variables it supports:
100+
101+
```php
102+
{{-- Example of General chip for a person, with data from a User model --}}
103+
@include('crud::chips.general', [
104+
'heading' => [
105+
'content' => $user->name,
106+
'href' => backpack_url('user/'.$user->id.'/show'),
107+
'title' => 'Click to preview',
108+
],
109+
'image' => [
110+
'content' => backpack_avatar_url($user), // doesn't work well with dummy data
111+
'element' => 'a',
112+
'href' => backpack_url('user/'.$user->id.'/show'),
113+
'title' => 'Because of dummy data, this image is not available, but it would show a profile image',
114+
],
115+
'details' => [
116+
[
117+
'icon' => 'la la-hashtag',
118+
'content' => $user->id,
119+
'url' => backpack_url('user/'.$user->id.'/show'),
120+
'title' => 'Click to preview',
121+
],
122+
[
123+
'icon' => 'la la-envelope',
124+
'content' => $user->email,
125+
'url' => 'mailto:'.$user->email,
126+
'title' => 'Click to email',
127+
],
128+
[
129+
'icon' => 'la la-calendar',
130+
'content' => $user->created_at->format('F j, Y'),
131+
'title' => 'Created at '.$user->created_at,
132+
]
133+
]
134+
])
135+
```
136+
137+
<a name="how-to-use-a-chip-as-a-datatable-column"></a>
138+
#### How to use a chip as a datatable column
139+
140+
![Backpack v7 general chip in datatable component](https://backpackforlaravel.com/uploads/v7/general_chip_in_datatable_component.jpg)
141+
142+
When your datatables have too many columns, chips become particularly useful. They allow you to compress the info from 5 or more columns... into a single chip column. This improves the UX of big datatables - your admins will no longer have to expand the table row or use horizontal scrolling to see crucial info.
143+
144+
Remember, a chip is just a simple blade file. So to use a chip as a column, we can just use the `view` column type, and pass the path to our chip file. For example:
145+
146+
```php
147+
// after we create an `invoice` chip
148+
// we can use that chip as a column:
149+
CRUD::addColumn([
150+
'name' => 'info',
151+
'type' => 'view',
152+
'view' => 'crud::chips.invoice',
153+
]);
154+
```
155+
156+
Now create that blade file, by running `php artisan backpack:chip invoice`. This will create a file in `resources/views/admin/chips` for you to edit, and customize as you like. By default, it just uses the `$entry` variable (which will be present if you use it as a column). You can include the `general` chip view if it's good enough for you, or copy-paste the HTML from the `general` chip, and modify it to your liking (you can run `php artisan backpack:chip invoice --from=general` to create a chip with all the HTML from general).
157+
158+
Please note:
159+
- If your chip uses any info from RELATED items, you should probably eager load those items. For example if you're in an InvoiceCrudController you could do this in your `setupListOperation()` or hell maybe even in setup(): `CRUD::with(['event', 'event.production', 'event.venue', 'event.venue.city']);` - that way when your chip needs that info, it already has it onpage, and makes no extra queries;
160+
- By default, the view column type is not searchable. In order to make your chip columns searchable you need to [specify a custom ```searchLogic``` in your declaration](/docs/{{version}}/crud-columns#custom-search-logic).
161+
- By default, the view column type is not orderable. In order to make your chip columns orderable you need to [specify a custom ```orderLogic``` in your declaration](/docs/{{version}}/crud-columns#custom-order-logic).
162+
163+
<a name="how-to-use-a-chip-as-a-widget"></a>
164+
#### How to use a chip as a widget
165+
166+
![Backpack v7 general chip as widget](https://backpackforlaravel.com/uploads/v7/general_chip_as_widget.jpg)
167+
168+
Chip files usually only contain the minimum content and styling necessary. You can include them as widgets directly, but they probably won't look very pretty on a custom page, because they don't have a background, borders, shadow etc. That's why we've also created a `chip` widget, which adds wrappers just like the other widgets - so that your chip will look good when placed on a custom page (or an existing CRUD page, why not).
169+
170+
To use the `chip` widget, you can do:
171+
172+
```php
173+
Widget::add()
174+
->to('after_content') // optional
175+
->type('chip')
176+
->view('crud::chips.owner')
177+
->title('Owner')
178+
->entry($owner);
179+
```
180+
181+
<hr>
182+
183+
<a name="overwriting-default-chip-types"></a>
184+
## Overwriting Default Chip Types
185+
186+
You can override a chip by create a file in ```resources\views\vendor\backpack\crud\chips``` with the same name. But it is NOT recommended to override the `general` chip type. When you do that, you're forfeiting any future updates for that chip. We can't push updates to a file that you're no longer using.
187+
188+
In 99.9% of the cases, it's recommended NOT to override the default `general` chip file, but to create a _custom_ chip file. That will make it a lot easier to upgrade to newer versions of Backpack - because the file is completely in your control.
189+
190+
<hr>
191+
192+
<a name="creating-a-custom-chip-type"></a>
193+
## Creating a Custom Chip Type
194+
195+
Chips consist of only one file - a blade file with the same name as the chip type (ex: ```person.blade.php```). You can create one by placing a new blade file inside ```resources\views\vendor\backpack\crud\chips```. Be careful to choose a distinctive name - usually the model name works best.
196+
197+
To create a new chip file in the standard directory, you can run:
198+
- `php artisan backpack:chip {chip-name}` to create a new file in that directory, from our stub that assumes you want to use that chip inside the ListOperation and ShowOperation, so you'll be using the `$entry` variable to define what you want the chip to include;
199+
- `php artisan backpack:chip {chip-name} --from=general` to create a new file in that directory, from our `general` chip, so you can change the HTML however you want;
200+
201+
For example, you can do `php artisan backpack:chip invoice` to create ```invoice.blade.php``` that helps you define what that chips includes:
202+
203+
```php
204+
@php
205+
$last_purchase = $entry->invoices()->orderBy('issuance_date', 'DESC')->first()->issuance_date;
206+
@endphp
207+
208+
@include('crud::chips.general', [
209+
'heading' => [
210+
'content' => 'Invoice '.$entry->series.' '.$entry->number.' - '.$entry->owner->name,
211+
'href' => backpack_url('pet-shop/invoice/'.$entry->id.'/show'),
212+
],
213+
'details' => [
214+
[
215+
'icon' => 'la la-dollar',
216+
'content' => $entry->total,
217+
'title' => 'Total invoice amount $'.$entry->total,
218+
],
219+
[
220+
'icon' => 'la la-tags',
221+
'content' => $entry->items->count().' items',
222+
],
223+
[
224+
'icon' => 'la la-calendar',
225+
'content' => $last_purchase->format('F j, Y'),
226+
'title' => 'Issuance date: '.$last_purchase,
227+
]
228+
]
229+
])
230+
```
231+
232+
But you can also run `php artisan backpack:chip custom --from=general`, wipe everything inside the generated file, and include your own custom HTML, hardcoded or not:
233+
234+
```html
235+
<div class="card mb-2">
236+
<div class="card-body">
237+
<div class="row align-items-center bp-chip">
238+
<div class="col-auto">
239+
<div class="d-block">
240+
<a href="https://google.com" title="Example of a chip" target="_blank" class="d-inline-block"><span class="avatar avatar-2 rounded" style="background-image: url(http://bp-v7-alpha7.test/uploads/person1.jpg)"> </span></a>
241+
</div>
242+
</div>
243+
<div class="col text-truncate">
244+
<div class="d-block">
245+
<a href="https://google.com" class="mb-1 d-inline-block " title="Example of a chip without URL" target="_blank">
246+
John Doe
247+
</a>
248+
</div>
249+
<div class="d-block text-secondary text-truncate mt-n1">
250+
<small class="d-inline-block me-1">
251+
<i class="la la-hashtag" title="Click to email"></i>
252+
<a href="mailto:john.doe@example.com" class="text-reset" title="Click to email">8AH13A7</a>
253+
</small>
254+
<small class="d-inline-block me-1">
255+
<i class="la la-envelope" title="Click to email"></i>
256+
<a href="mailto:john.doe@example.com" class="text-reset" title="Click to email">john.doe@example.com</a>
257+
</small>
258+
<small class="d-inline-block me-1">
259+
<i class="la la-phone" title="Click to call"></i>
260+
<a href="tel:+15551234567" class="text-reset" title="Click to call">+1 (555) 123-4567</a>
261+
</small>
262+
</div>
263+
</div>
264+
</div>
265+
</div>
266+
```
267+
268+
Otherwise, you can create a completely custom chip, that looks and works differently from the `general` chip, and re-use that in your application. There are no limitations - since chips are simple blade files. Just copy-paste the HTML from the `general` chip and change it to match your needs.

7.x-dev/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [3. Advanced Features](/docs/{{version}}/getting-started-advanced-features)
1717
- [4. Add-ons, License & Support](/docs/{{version}}/getting-started-license-and-support)
1818
- [Tutorials](/docs/{{version}}/tutorials)
19-
19+
2020
#### Admin UI
2121

2222
- [About](/docs/{{version}}/base-about)
@@ -36,6 +36,7 @@
3636
+ [Columns](/docs/{{version}}/crud-columns)
3737
+ [Buttons](/docs/{{version}}/crud-buttons)
3838
+ [Filters](/docs/{{version}}/crud-filters)
39+
+ [Chips](/docs/{{version}}/crud-chips)
3940
+ [Create](/docs/{{version}}/crud-operation-create) & [Update](/docs/{{version}}/crud-operation-update)
4041
+ [Fields](/docs/{{version}}/crud-fields)
4142
+ [Save Actions](/docs/{{version}}/crud-save-actions)
@@ -44,6 +45,7 @@
4445
+ [Delete](/docs/{{version}}/crud-operation-delete)
4546
+ [Show](/docs/{{version}}/crud-operation-show)
4647
+ [Columns](/docs/{{version}}/crud-columns)
48+
+ [Chips](/docs/{{version}}/crud-chips)
4749
- [Additional Operations](/docs/{{version}}/crud-operations)
4850
+ [Clone](/docs/{{version}}/crud-operation-clone)
4951
+ [Reorder](/docs/{{version}}/crud-operation-reorder)

0 commit comments

Comments
 (0)