Skip to content

Commit e8bcbf1

Browse files
committed
Update readme with more usage examples and document how to customize partition links.
1 parent 10fcdcb commit e8bcbf1

File tree

1 file changed

+134
-17
lines changed

1 file changed

+134
-17
lines changed

README.md

Lines changed: 134 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/saintsystems/nova-linkable-metrics.svg?style=flat-square)](https://packagist.org/packages/saintsystems/nova-linkable-metrics)
44
[![Total Downloads](https://img.shields.io/packagist/dt/saintsystems/nova-linkable-metrics.svg?style=flat-square)](https://packagist.org/packages/saintsystems/nova-linkable-metrics)
55

6-
Add custom router-links to your Laravel Nova metrics.
6+
Add custom links to your Laravel Nova metrics.
77

88
![screenshot](screenshot.gif)
99

@@ -19,30 +19,45 @@ composer require saintsystems/nova-linkable-metrics
1919

2020
To add the link ability to your Laravel Nova metric cards, you need to add the `Linkable` traits to your metrics.
2121

22-
For example, within your custom Nova value metric card:
22+
For example, within your custom Nova value metric:
2323
```php
24-
// in your Nova value metric card class:
25-
use SaintSystems\Nova\LinkableMetrics\LinkableValue;
24+
// App\Nova\Metrics\NewUsers.php
2625

27-
use LinkableValue;
26+
use SaintSystems\Nova\LinkableMetrics\LinkableValue;
27+
28+
class NewUsers extends Value
29+
{
30+
use LinkableValue;
31+
32+
//...omitted for brevity
2833

2934
```
3035

31-
Within your custom Nova trend metric card:
36+
Within your custom Nova trend metric:
3237
```php
33-
// in your Nova value metric card class:
34-
use SaintSystems\Nova\LinkableMetrics\LinkableTrend;
38+
// App\Nova\Metrics\UsersPerDay.php
3539

36-
use LinkableTrend;
40+
use SaintSystems\Nova\LinkableMetrics\LinkableTrend;
41+
42+
class UsersPerDay extends Trend
43+
{
44+
use LinkableTrend;
45+
46+
//...omitted for brevity
3747

3848
```
3949

40-
Within your custom Nova trend partition card:
50+
Within your custom Nova partition metric:
4151
```php
42-
// in your Nova value metric card class:
43-
use SaintSystems\Nova\LinkableMetrics\LinkablePartition;
52+
// App\Nova\Metrics\UsersByStatus.php
53+
54+
use SaintSystems\Nova\LinkableMetrics\LinkablePartition;
55+
56+
class UsersByStatus extends Partition
57+
{
58+
use LinkablePartition;
4459

45-
use LinkablePartition;
60+
//...omitted for brevity
4661

4762
```
4863

@@ -65,14 +80,41 @@ You can define metric links using the `route` method from the `Linkable` trait i
6580
protected function cards()
6681
{
6782
return [
68-
(new TotalUsers)->width('1/3')->route('nova.pages.index', ['resource' => 'users']),
83+
(new NewUsers)->width('1/3')->route('nova.pages.index', ['resource' => 'users']),
6984
];
7085
}
7186
```
7287

7388
**OR using a Lens Route**
7489

7590
```php
91+
// App\Nova\Lenses\UnverifiedEmail.php
92+
93+
class UnverifiedEmail extends Lens
94+
{
95+
//... omitted for brevity
96+
97+
public static function query(LensRequest $request, $query)
98+
{
99+
return $request->withOrdering($request->withFilters(
100+
$query->whereNull('email_verified_at')
101+
));
102+
}
103+
104+
//... omitted for brevity
105+
106+
/**
107+
* Get the URI key for the lens.
108+
*
109+
* @return string
110+
*/
111+
public function uriKey()
112+
{
113+
return 'unverified-email';
114+
}
115+
116+
}
117+
76118
// App\Nova\Dashboards\Main.php
77119

78120
/**
@@ -83,14 +125,47 @@ You can define metric links using the `route` method from the `Linkable` trait i
83125
protected function cards()
84126
{
85127
return [
86-
(new TotalUsers)->width('1/3')->route('nova.pages.lens', ['resource' => 'users', 'lens' => 'active-users']),
128+
(new NewUsers)->width('1/3')->route('nova.pages.lens', ['resource' => 'users', 'lens' => 'unverified-email']),
87129
];
88130
}
89131
```
90132

91133
**OR using a Filter Route**
92134

93135
```php
136+
// App\Nova\Filters\UserStatus.php
137+
138+
class UserStatus extends Filter
139+
{
140+
//... omitted for brevity
141+
142+
/**
143+
* Apply the filter to the given query.
144+
*
145+
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
146+
* @param \Illuminate\Database\Eloquent\Builder $query
147+
* @return \Illuminate\Database\Eloquent\Builder
148+
*/
149+
public function apply(NovaRequest $request, $query, $value)
150+
{
151+
return $query->where('active', $value);
152+
}
153+
154+
/**
155+
* Get the filter's available options.
156+
*
157+
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
158+
* @return array
159+
*/
160+
public function options(NovaRequest $request)
161+
{
162+
return [
163+
'Active' => '1',
164+
'Inactive' => '0'
165+
];
166+
}
167+
}
168+
94169
// App\Nova\Dashboards\Main.php
95170

96171
/**
@@ -103,12 +178,12 @@ You can define metric links using the `route` method from the `Linkable` trait i
103178
$filter = base64_encode(json_encode([
104179
[
105180
'class' => UserStatus::class,
106-
'value' => 'active',
181+
'value' => '1',
107182
],
108183
]));
109184

110185
return [
111-
(new TotalUsers)->width('1/3')->route('nova.pages.index', ['resource' => 'users', 'users_filter' => $filter]),
186+
(new NewUsers)->width('1/3')->route('nova.pages.index', ['resource' => 'users', 'users_filter' => $filter]),
112187
];
113188
}
114189
```
@@ -138,6 +213,48 @@ You can define metric links using the `route` method from the `Linkable` trait i
138213
}
139214
```
140215

216+
**Customizing Partition Links**
217+
218+
By default, Partition metrics can have links just like Value and Trend metrics. However, using the default `route` method like on Value and Trend metrics (as shown above) will simply link the PartitionMetric card title to the provided route/url.
219+
220+
For greater customization, you may pass a Closure to the new `link` method on the LinkablePartitionResult class that allows you to customize the link for each individual partition in the generated chart, and even pass partition information to the route like below:
221+
222+
```php
223+
// App\Nova\Metrics\UsersByStatus
224+
225+
use SaintSystems\Nova\LinkableMetrics\LinkablePartition;
226+
227+
class UsersByStatus extends Partition
228+
{
229+
use LinkablePartition;
230+
231+
/**
232+
* Calculate the value of the metric.
233+
*
234+
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
235+
* @return mixed
236+
*/
237+
public function calculate(NovaRequest $request)
238+
{
239+
return $this->count($request, User::class, 'active')
240+
->label(fn ($value) => match ($value) {
241+
1 => 'Active',
242+
0 => 'Inactive'
243+
})
244+
->link(fn ($value) => match ($value) {
245+
// 1 => null,
246+
default => route('nova.pages.index', ['resource' => 'users', 'users_filter' => base64_encode(json_encode([
247+
[
248+
'class' => UserStatus::class,
249+
'value' => $value,
250+
],
251+
]))], false)
252+
});
253+
}
254+
255+
// ... omitted for brevity
256+
```
257+
141258
## Credits
142259

143260
- [Adam Anderly](https://github.com/anderly)

0 commit comments

Comments
 (0)