【问题标题】:How to get http Request for a Link when click单击时如何获取链接的http请求
【发布时间】:2015-10-27 14:44:17
【问题描述】:

我正在做一个标签系统,当用户点击某个链接时,显示与之相关的所有结果

鉴于我有这样的链接

@foreach($productTags as $tags)
    <a href="{{ action('Product\ProductController@searchTags', ['tags_id' => $tags->id]) }}" id="tags">{{$tags->tags}}</a>
@endforeach

路线

Route::get('tags/{tags}','Product\ProductController@searchTags');

我想把它放在像这样的控制器中

public function searchTags($tags)
       {
        dd($request->get('tags_ids'));
         $search = \DB::table('products')->select('tags','id')
                               ->where('tags',$tags)->get();
                               return view('products.show.search',
                                                            compact('search'));
       }

但我得到null

【问题讨论】:

    标签: php jquery laravel search tags


    【解决方案1】:

    我不确定您要完成什么。当您单击标签时,您应该看到带有该标签的产品吗?

    这里有几件事,我会指出。

    您正在混合使用 GETPOSTsearchTags(Request $request) 接受 Request,就好像正在向它提交表单一样。但是路由是GET,您视图中的链接将产生GET 请求。

    假设您想点击一个标签并查看也有该标签的产品,您可以尝试以下操作:

    products/show.blade.php:

    @foreach($productTags as $tag)
      <a href="/tags/{{ $tag->id }}">{{ $tag->name }}</a>
    @endforeach
    

    routes.php:

    Route::get('tags/{id}', 'TagsControlller@show');
    

    TagsController.php:

    /**
     * Display products with tag 
     *
     * @param $id
     * @return Response
     */
     public function show($id)
     {
         $products = Product::with('tags')->where(['id' => $id])->get();
         return view('tags.show')->with('products', $products);
     }
    

    【讨论】:

      【解决方案2】:

      在您的产品控制器中

      public function searchTags($tag_id) 
      {
               $search = \DB::table('products')->select('tags','id')
                    ->where('tag_id','=', $tag_id)->get();
               return view('products.show.search',
                    compact('search'));
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 2023-04-07
        相关资源
        最近更新 更多