[英]How to create dynamic route link in my navbar
I want to create dynamic route on my navbar and redirect it to http://localhost:8000/tasks/nenad page 我想在导航栏上创建动态路由,并将其重定向到http:// localhost:8000 / tasks / nenad页面
Web.php Web.php
Route::get('/tasks/{first_name}', 'Viewercontroller@profile')
->middleware('viewer')
->name('profile');
ViewerController ViewerController
public function profile($first_name) {
$user = User::whereFirst_name($first_name)->first();
return view('viewers/tasks', compact('user'));
}
Navbar.blade.php: Navbar.blade.php:
<li><a href="{{ url(route('profile')) }}">Tasks</a></li>
I know that i have to change link on my navbar page but doesn't know how, any help will be great... 我知道我必须更改导航栏页面上的链接,但不知道如何,任何帮助都将非常有用...
it won't work because your route expects a parameter, so you have to indicate on your navbar.blade.php the parameter you want to pass. 由于您的路线需要一个参数,因此该参数将无法正常工作,因此您必须在navbar.blade.php中指定要传递的参数。
For example I'll assume you have a user saved on $user, then you would do this: 例如,我假设您有一个用户保存在$ user上,那么您可以这样做:
route('profile', ['first_name' => $user->name]);
You can read more about URL generation on Laravel docs 您可以在Laravel文档上阅读有关URL生成的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.