【问题标题】:Want to add {{$value['category']}} in <a href="{{route(' HERE ')}}">{{$value['category']}}</a> in Laravel想在 Laravel 的 <a href="{{route(' HERE ')}}">{{$value['category']}}</a> 中添加 {{$value['category']}}
【发布时间】:2020-04-06 08:41:18
【问题描述】:

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

这是我的代码:

@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。路线是什么。谢谢。

【问题讨论】:

    标签: php laravel laravel-5 laravel-4


    【解决方案1】:

    路线:

    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){ 
        $all = product::all()->where('category', 'db_val_1');
        return view('homepage', ['details'=> $all]);
    }
    public function db_val_2(Request $req){ 
        $all = product::all()->where('category', 'db_val_2');
        return view('homepage', ['details'=> $all]);
    }
    public function db_val_3(Request $req){ 
        $all = product::all()->where('category', 'db_val_3');
        return view('homepage', ['details'=> $all]);
    }
    ...
    

    查看主页:在route 中,值将是($value-&gt;category),如下所示。

    @foreach($details as $value)
        <a href="{{route($value->category)}}"> {{$value['category']}}</a>
    @endforeach
    

    我得到了解决方案。感谢您的帮助。

    【讨论】:

    • 如果它适合你,没关系...但最好只使用一个带参数的路由... Route::get('/homepage/{dbv}', 'HomeController@ db_val')->name('db_val');在控制器中,您可以使用 public function db_val(Request $request,$dbv) { ... use $dbv... 获取 {dbv} 参数,它将是 db_val1 或 db_val2 或 db_valxx 或您在网址 www.xxxx 中输入的内容。 com/homepage/db_what_you_want ,并在视图中使用 url 生成:{{ route('db_val') , 'db_what_you_want' }}
    【解决方案2】:

    1) 警告:您在 HomeController 中调用了 db_val 函数,但您在问题中显示了 viewhome 方法。

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

    如果你想使用方法 viewhome :

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

    2) route 与命名路由一起使用
    你有一个名为 'db_val' 的路由 --> Route::.... ->name('db_val');
    所以必须这样使用,

    <a href='{{route('db_val')}}
    

    3) 在您的情况下,假设 foreach 中的 $value 是一个内部带有“类别”索引的数组 你可以用url代替route

    @foreach($details as $value)
        <a href="{{url('/your_url')}}/{{$value['category']}}"> 
          link to {{$value['category']}} 
        </a>
    @endforeach
    

    4) 但刀灵是

    路由.php

    Route::get('/showcategory/{id}','HomeController@showcategorie')->name('showcat');
    

    查看

       @foreach($details as $value)
             <a href="{{route('showcat', $value['category'])}}">
        @endforeach
    

    这意味着:你有一个名为/showcategory 的路由,带有一个参数/{id}

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2017-02-14
      • 2022-10-25
      • 1970-01-01
      • 2023-01-09
      • 2017-06-07
      相关资源
      最近更新 更多