laravel blade creating url

若如初见. 提交于 2020-05-26 08:57:46

问题


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]) }}



来源:https://stackoverflow.com/questions/42270100/laravel-blade-creating-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!