Skip to main content
The conditional-link component wraps content in a link only when certain conditions are met, such as user permissions. It’s particularly useful for displaying content that should only be clickable for authorized users.

Basic Usage

<x-forms::conditional-link url="{{ route('users.show', $user) }}" :show-link="true">
    {{ $user->name }}
</x-forms::conditional-link>
Use the can attribute to show links only for authorized users:
<x-forms::conditional-link 
    url="{{ route('posts.edit', $post) }}"
    can="update"
    :arg="$post"
>
    Edit Post
</x-forms::conditional-link>

Model Binding

Automatically display model attribute values:
<x-forms::conditional-link 
    url="{{ route('users.show', $user) }}"
    name="name"
    :model="$user"
    can="view"
    :arg="$user"
/>

Value Formatting

Status Enums

The component automatically detects status enums and displays them with colors:
<x-forms::conditional-link 
    url="{{ route('orders.show', $order) }}"
    :value="$order->status"
>
</x-forms::conditional-link>

Multiline Text

Display text with line breaks preserved:
<x-forms::conditional-link 
    url="#"
    :value="$description"
    :multiline="true"
    :show-link="false"
/>

Array Values

Arrays are automatically joined with a separator:
<x-forms::conditional-link 
    url="#"
    :value="['tag1', 'tag2', 'tag3']"
    :show-link="false"
/>

Custom Content

Override default rendering with slot content:
<x-forms::conditional-link 
    url="{{ route('products.show', $product) }}"
    can="view"
    :arg="$product"
>
    <strong>{{ $product->name }}</strong>
    <span class="text-muted">{{ $product->sku }}</span>
</x-forms::conditional-link>

Attributes

url
string
required
The URL to link to when the link is shown
name
string
default:""
The model attribute name to display when no value is provided
Explicitly control whether to show the link. When null, uses the can attribute to determine visibility
can
string
default:""
The permission to check. Link is only shown if the user has this permission
arg
mixed
default:"[]"
Arguments to pass to the permission check (typically the model instance)
guard
string
default:"null"
The authentication guard to use for permission checking
value
mixed
default:"null"
The value to display. If null, uses the bound value from the model
model
mixed
default:"null"
The model instance to extract values from when using the name attribute
multiline
bool
default:"false"
Whether to preserve line breaks in the displayed text
framework
string
default:""
The CSS framework to use for rendering

Display Logic

The component follows this rendering logic:
  1. If slot content is provided, it’s displayed
  2. If the value is an AdminModel, displays admin_link_name
  3. If the value is a status enum, renders as a status badge with color
  4. If multiline is true, preserves line breaks
  5. If the value is an array, joins elements with a separator
  6. Otherwise, formats the value using default formatting

Examples

@foreach($users as $user)
    <tr>
        <td>
            <x-forms::conditional-link 
                url="{{ route('users.edit', $user) }}"
                can="update"
                :arg="$user"
            >
                {{ $user->name }}
            </x-forms::conditional-link>
        </td>
    </tr>
@endforeach
<x-forms::conditional-link 
    url="{{ route('orders.show', $order) }}"
    :value="$order->status"
    can="view"
    :arg="$order"
/>

Build docs developers (and LLMs) love