laravel blade creating url

2020-07-25 10:17发布

问题:

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:

@foreach ($websites as $website)
<a class="websites" href=" {{  asset ($website->name )}}"> {{ asset ($website->name )}}
</a> @endforeach

Which gives for example: http://localhost/name

Howver links needs to be like this:

http://localhost/website/name how can I add /website into my URL using blade template in laravel?

回答1:

Try this:

{{ url('website/' . $website->name) }}


回答2:

This have some improvement on @Laran answer regarding best practices.

You would better use url parameters instead of concatenating the $name parameter

{{ url('website', [$name]) }}

And using named routes will be better to decouple the routing from the views.

// routes/web.php
Route::get('website')->name('website');

and write inside your {{ route('website', [$name]) }}