To generate and display URLs and links using Blade in Laravel, you can use the url()
helper function and the @
symbol in Blade directives. Here are some examples:
<a href="{{ url('path/to/page') }}">Link</a>
<a href="{{ url('users', ['id' => 1]) }}">User Profile</a>
This will generate a URL like /users?id=1
.
<a href="{{ route('route.name') }}">Named Route</a>
Make sure to define the named route in your web.php
routes file using the name()
method.
<a href="{{ url('path/to/page', ['param1' => 'value1', 'param2' => 'value2']) }}">Link</a>
This will generate a URL like /path/to/page?param1=value1¶m2=value2
.
<a href="{{ url('path/to/page') }}#section">Link with Anchor</a>
This will generate a URL like /path/to/page#section
.
Note: Remember to use the {!! !!}
directive to render the link as HTML in Blade templates to prevent HTML encoding.
These are some of the ways you can generate and display URLs and links using Blade in Laravel.