【问题标题】:Laravel show product page link on a dynamic navbarLaravel 在动态导航栏上显示产品页面链接
【发布时间】:2018-07-03 03:40:00
【问题描述】:

我目前是 laravel 的新手,我尝试创建一个导航栏,其中包含 3 个类别(cat_id=8 的衬衫)、(cat_id=9 的裤子)和(cat_id=10 的连衣裙)。

我在两个表中都有通过cat_id 列连接的产品表和类别表。

我试图做到这一点,当用户点击类别下的产品链接时,它会将其留给该产品视图,并带有自己的pro_id

这是我的控制器,主页面有变量

 public function index()
    {
        $pro_shirt=Products::where('cat_id','8')->get();
        $pro_pant=Products::where('cat_id','9')->get();
        $pro_dress=Products::where('cat_id','10')->get();
        return view('front.home',compact('pro_shirt','pro_pant','pro_dress'));
    }

这里是我的产品页面链接路径

Route::get('/product/{id}',['as' => 'front.product.show', 'uses' => 'HomeController@product']);

该产品页面的控制器功能

   public function product($id){
        $product = Products::where('pro_id',$id)->Paginate(1);
        return view('front.product.product', compact('product'));
    }

这是我在刀片大师视图中的导航栏

<div class="col1">
                                    <div class="h_nav">
                                        <a href="{{route('front.category.show', ['id' =>8]) }}"><h4>Shirt</h4></a>
                                        <ul>
                                        @foreach($pro_shirt as $shirt)
                                              <li><a href="{{route('front.product.show'),['id' =>$shirt->pro_id]}}">{{$shirt->pro_title}}</a></li>
                                        @endforeach
                                        </ul>
                                    </div>
                                </div>
                                <div class="col1">
                                    <div class="h_nav">
                                        <a href="{{route('front.category.show', ['id' =>9]) }}"><h4>Pant</h4></a>
                                        <ul>
                                        @foreach($pro_pant as $pant)
                                              <li><a href="{{route('front.product.show'),['id' =>$pant->pro_id]}}">{{$pant->pro_title}}</a></li>
                                        @endforeach
                                        </ul>
                                    </div>
                                </div>
                                <div class="col1">
                                    <div class="h_nav">
                                        <a href="{{route('front.category.show', ['id' =>10]) }}"><h4>Dress</h4></a>
                                        <ul>
                                        @foreach($pro_dress as $dress)
                                              <li><a href="{{route('front.product.show'),['id' =>$dress->pro_id]}}">{{$dress->pro_title}}</a></li>
                                        @endforeach
                                        </ul>
                                    </div>
                                </div>

当我尝试点击每个产品链接时,它会显示一个错误:

缺少 [Route: front.product.show] [URI: product/{id}] 的必需参数。 (查看:C:\xampp\htdocs\webshop\resources\views\front\extends\master.blade.php)

我不知道为什么该产品的 pro_id 没有显示,但 pro_title 显示了

更新:我错过了路线中的关闭),因此导航栏现在可以工作

【问题讨论】:

    标签: php laravel-5 routing


    【解决方案1】:

    你的观点有误, 而不是{{route('front.product.show'),['id' =&gt;$shirt-&gt;pro_id]}}需要 {{route('front.product.show',['id' =&gt;$shirt-&gt;pro_id])}}.
    对其余路由 front.product.show 执行相同操作。

    【讨论】:

    • 天哪,它现在工作了,谢谢:D,我猜我打字的时候眼睛累了:D。
    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2011-11-25
    • 1970-01-01
    相关资源
    最近更新 更多