在Laravel中通过GET传递ID的问题

时间:2019-12-26 14:14:22

标签: laravel routing

Laravel框架6.9 + XAMP

我需要Laravel经验丰富的人的建议,因为我有一些无法解释的奇怪问题。

刀片模板具有一个按钮,该按钮应通过产品ID将商品添加到购物车:

<div class="buy d-flex justify-content-between align-items-center">
     <div class="price text-success"><h5 class="mt-4">$ {{$product->price}}</h5></div>
     <a href="{{route('product.addToCart', ['id'=>$product->id] )}}" class="btn btn-danger mt-3"><i class="fas fa-shopping-cart"></i> Add to Cart</a>
</div>

但是,单击按钮时什么也没有发生。没有传递产品ID,仅传递#。

这是我称之为的web.php路由:

    Route::get('add-to-cart/{id}', [
    'uses' => 'ProductController@getAddToCart',
    'as' => 'product.addToCart'
]);

也尝试过:

//Route::get('add-to-cart/{id}', 'ProductController@getAddToCart')->name('product.addToCart'); 

这也不起作用。

这是购物车型号:

<?php
namespace App;
class Cart
{
    public $items = null;
    public $totalQty = 0;
    public $totalPrice = 0;


    public function __construct($oldCart)
    {
        if ($oldCart) {
            $this->items = $oldCart->items;
            $this->totalQty = $oldCart->totalQty;
            $this->totalPrice = $oldCart->totalPrice;
        }
    }

    public function add($item, $id) {
        $storedItem = ['qty' => 0, 'price' => $item->price, 'item'=> $item];
        if ($this->items) {
            if (array_key_exists($id, $this->items)) {
                $storedItem = $this->items[$id];
            }
        }
        $storedItem['qty']++;
        $storedItem['price'] = $item->price * $storedItem['qty'];
        $this->items[$id] = $storedItem;


        $this->totalQty++;
        $this->totalPrice += $item->price;
    }
}

这是产品控制器:

namespace App\Http\Controllers;

use App\Product;

use Illuminate\Http\Request;
use App\Cart;
use Session;

class ProductController extends Controller
{

    public function index()
    {
        $products = Product::all();
        return view('shop.index', ['products' => $products]);
    }

    public function getAddToCart(Request $request, $id) {
        $product = Product::find($id);
        $oldCart = Session::has('cart') ? Session::get('cart') : null;

        $cart = new Cart($oldCart);
        $cart->add($product, $product->id);

        $request->session()->put('cart', $cart);
        //dd($request->session()->get('cart'));
        return redirect()->route('index');
    }
}

如果我仅输入URL:/ add-to-cart / {prod id}并按Enter,它就可以工作-我可以看到'cart'的dd()以及产品详细信息,因此路由有效。只有按钮没有!

让我发疯。我究竟做错了什么?我不知道Laravel有一些“功能”吗?

    dd($product)
App\Product {#279 ▼
  #fillable: array:3 [▼
    0 => "title"
    1 => "description"
    2 => "price"
  ]
  #connection: "mysql"
  #table: "products"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [▼
    "id" => 2
    "created_at" => "2019-12-22 18:05:40"
    "updated_at" => "2019-12-22 18:05:40"
    "imagePath" => "https://s3.eu-central-1.amazonaws.com/bootstrapbaymisc/blog/24_days_bootstrap/vans.png"
    "title" => "Neque quia sit facere."
    "description" => "Seven flung down his cheeks, he went on growing, and, as the door opened inwards, and Alice's elbow was pressed hard against it, that attempt proved a failure.  ▶"
    "price" => 29
  ]
  #original: array:7 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

购物车中装满了产品。

1 个答案:

答案 0 :(得分:0)

从我所看到的看来,您的路由未正确重定向。在您的控制器中,操作定义为

public function getAddToCart(Request $request, $id)

,但是在您的按钮中,您仅传递了id参数。

尝试将控制器中的方法重新定义为

public function getAddToCart($id)

应该可以解决问题的