|
| 1 | +--- |
| 2 | +title: Conditional Link |
| 3 | +sidebar_position: 11 |
| 4 | +--- |
| 5 | + |
| 6 | +You can use the `conditional-link` component to render `<a>` tags conditionally. This can be useful if you want to link to something only if a user has the permission to do so. |
| 7 | + |
| 8 | +## Basic usage |
| 9 | + |
| 10 | +```bladehtml |
| 11 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" :show-link="$your_condition_boolean" value="10" /> |
| 12 | +``` |
| 13 | + |
| 14 | +## Using `can` |
| 15 | + |
| 16 | +Instead of directly using the `show-link` prop, you can also check for a user permission by using the `can` and `arg` props. |
| 17 | + |
| 18 | +```bladehtml |
| 19 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" can="view" arg="users" value="10" /> |
| 20 | +``` |
| 21 | + |
| 22 | +You can also specify a specific `guard` when using `can` |
| 23 | + |
| 24 | +```bladehtml |
| 25 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" can="view" arg="users" guard="web" value="10" /> |
| 26 | +``` |
| 27 | + |
| 28 | +## Using slot |
| 29 | + |
| 30 | +You can also render your data manually instead of using the `value` prop. |
| 31 | + |
| 32 | +```bladehtml |
| 33 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" can="view" arg="users"> |
| 34 | + {{ $organization->users_count }} |
| 35 | +</x-forms::conditional-link> |
| 36 | +``` |
| 37 | + |
| 38 | +## Using model value binding |
| 39 | + |
| 40 | +You can also bind to a model attribute value by using `name` prop when in a model context. |
| 41 | + |
| 42 | +```bladehtml |
| 43 | +@model($organization) |
| 44 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" can="view" arg="users" name="users_count" /> |
| 45 | +@endmodel |
| 46 | +``` |
| 47 | + |
| 48 | +## Passing additional HTML attributes |
| 49 | + |
| 50 | +HTML attributes will get rendered on the `a` tag. For example, to set the `target` to `_blank`, just use the `target` attribute. |
| 51 | +Remember, if the `show-link` evaluates to `false`, then these extra attributes won't be rendered. |
| 52 | + |
| 53 | +```bladehtml |
| 54 | +<x-forms::conditional-link :url="route('admin.users.index', $organization)" can="view" arg="users" value="10" target="_blank" /> |
| 55 | +``` |
| 56 | + |
| 57 | +## Allowed props |
| 58 | + |
| 59 | +The `conditional-link` support the following props: |
| 60 | +- `'url'` - (Required) the url to link to. |
| 61 | +- `'show-link'` - (Bool) Whether to render as a link or not |
| 62 | +- `'value'` - (Optional) Value to render in the link |
| 63 | +- `'name'` - Name of the attribute, if using model value binding |
| 64 | +- `'can'` - Ability to check for, instead of using `show-link` |
| 65 | +- `'arg'` - Argument to pass to `can` method |
| 66 | +- `'guard'` - User guard to use when using `can`. By default uses the current guard. |
| 67 | +- `'multiline'` - (Bool) If the bound value should be rendered as multiline |
| 68 | +- `framework` - Which css framework to use. |
0 commit comments