【问题标题】:Laravel - Passing id as parameter but the route gets the key, not the valueLaravel - 将 id 作为参数传递,但路由获取键,而不是值
【发布时间】:2016-05-25 10:04:59
【问题描述】:

所以我遇到了一个非常奇怪的问题。我像这样传递我的参数:

<form action="{{ route('deleteCustomerCartItem', ['product_id', $cartItem['product']->id, 'size' => $cartItem['size']]) }}" method="post">

我正在我的购物车页面中打印$cartItem['product']-&gt;id,它会打印有效的 ID。但是,当我通过它并 var_dump 它时,我得到字符串(10)“product_id”。我注意到我的路线没有得到实际的 id,但它得到了“product_id”。这是我的路线:

Route::post('/cart/delete/{product_id}/{size?}', [
'uses' => 'CartController@deleteCustomerItem',
'as' => 'deleteCustomerCartItem']);

这是我从这条路线获得的网址:

product_id 必须为 1,S 为 $cartItem['size']。我不知道为什么会发生这种情况,或者为什么最后会出现“?1”。

我需要获取 product_id 的值而不是键。有人可以解释为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:
    ['product_id' => $cartItem['product']->id, 'size' => $cartItem['size']]
    
                  ^
                  |
    // Note the use of => here instead of ,
    

    【讨论】:

    • 我没有注意到我使用了逗号。谢谢!
    • Ben 是对的,通过使用逗号,您实际上是在创建一个包含两个元素的数组。第一个元素是键的字符串名称,第二个是你想要的值。框架采用了第一个元素。
    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2019-03-03
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2023-03-07
    相关资源
    最近更新 更多