类AppModelsProduct的对象无法转换为int



如果我想增加按钮>Product quantity显示一个警告消息,如果我的Product details.blade.php:

<div class="quantity" style="margin-top: 10px;">
<span>Quantity:</span>
<div class="quantity-input">
<input type="text" name="product-quatity" value="1" data-max="120" pattern="[0-9]*" wire:model="qty" >
<a class="btn btn-reduce" href="#" wire:click.prevent="decreaseQuantity"></a>
<a class="btn btn-increase" href="#" wire:click.prevent="increaseQuantity"></a>
@if(Session::has('message'))
<div class="alert alert-danger" role="alert">{{ Session::get('message') }}</div>
@endif 
</div>
</div>

和我的产品详细信息控制器:

public function increaseQuantity(Product $product_quantity)
{
if($this->qty >= $product_quantity) {
$this->qty++;
}
else {
session()->flash('message', 'No stock available!');
}
}

您正在使用路由模型绑定:
public function increaseQuantity(Product $product_quantity)所以Laravel传递产品模型实例,这意味着在你的代码中,$product_quantity是一个基于你在URL上发送的数字的产品模型。
由于我们不知道你的模型和它的字段,我可以说,如果product_quantity是产品模型中的一个字段,那么你需要将代码更改为:
if($this->qty >= $product_quantity->product_quantity)

如果你想直接从URL发送product_quantity,你需要删除Product作为参数类型,并添加int:
public function increaseQuantity(int $product_quantity)

最新更新