猿问

想在 Laravel 的 <a href="{{route(' HERE ')}}">

我想{{$value['category']}}设置{{route('')}}


这是我的代码:


@foreach($details as $value)

    <a href="{{route(' **Here** ')}}"> {{$value['category']}}</a>

@endforeach

控制器功能:


public function viewhome(Request $req){

    $product = product::all()->unique('category');

    return view('homepage',['details'=> $product]);}

路线:


Route::get('/homepage/db_val', 'HomeController@db_val')->name('db_val');

如何href正确申报。路线是什么。谢谢你。


慕侠2389804
浏览 121回答 2
2回答

慕桂英546537

1)警告:您调用db_val函数HomeController但在您的问题中显示 viewhome 方法。Route::get('/homepage/db_val', 'HomeController@<b>db_val</b>')->name('db_val');&nbsp;如果你想使用方法 viewhome :Route::get('/homepage/db_val', 'HomeController@<b>viewhome</b>')->name('db_val');2)路由与命名路由一起使用你有一个名为 'db_val' 的路由 --> Route::.... ->name('db_val');所以必须这样使用,<a href='{{route('db_val')}}3)在您的情况下,假设$valueinforeach是一个内部带有“类别”索引的数组,您可以url改用route@foreach($details as $value)&nbsp; &nbsp; <a href="{{url('/your_url')}}/{{$value['category']}}">&nbsp;&nbsp; &nbsp; &nbsp; link to {{$value['category']}}&nbsp;&nbsp; &nbsp; </a>@endforeach4) 但刀锋精神是路由.phpRoute::get('/showcategory/{id}','HomeController@showcategorie')->name('showcat');看法&nbsp; &nbsp;@foreach($details as $value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="{{route('showcat', $value['category'])}}">&nbsp; &nbsp; @endforeach/showcategory这意味着:您有一个带有参数的命名路由/{id}

ABOUTYOU

路线:Route::get('/homepage/db_val_1', 'HomeController@db_val_1')->name('db_val_1');Route::get('/homepage/db_val_2', 'HomeController@db_val_2')->name('db_val_2');Route::get('/homepage/db_val_3', 'HomeController@db_val_3')->name('db_val_3');...控制器功能:public function db_val_1(Request $req){&nbsp;&nbsp; &nbsp; $all = product::all()->where('category', 'db_val_1');&nbsp; &nbsp; return view('homepage', ['details'=> $all]);}public function db_val_2(Request $req){&nbsp;&nbsp; &nbsp; $all = product::all()->where('category', 'db_val_2');&nbsp; &nbsp; return view('homepage', ['details'=> $all]);}public function db_val_3(Request $req){&nbsp;&nbsp; &nbsp; $all = product::all()->where('category', 'db_val_3');&nbsp; &nbsp; return view('homepage', ['details'=> $all]);}...查看首页:route中值会是($value->category)这样的。@foreach($details as $value)&nbsp; &nbsp; <a href="{{route($value->category)}}"> {{$value['category']}}</a>@endforeach我得到了解决方案。感谢您的帮助。
随时随地看视频慕课网APP
我要回答