在 Laravel 中更改布尔值时遇到奇怪的问题

问题描述 投票:0回答:1

所以在 laravel 中遇到了这个奇怪的问题。 我正在尝试更新数据库中的布尔值,但我的日志中不断收到这个奇怪的错误:

[路线:change.stock][URI:product/stock/{product}/{turn}]缺少必需参数[缺少参数:turn]

也许有人知道我在这里看到了什么。

我的路线:

Route::get('/product/stock/{product}/{turn}', [ProductController::class, 'changeStock'])->name('change.stock');

我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Requests\Product\{ImageRequest,OfferRequest,DeliveryRequest,InformationsRequest};
use App\Models\{Product,Image,Offer,Delivery,Category,Favorite};
use Illuminate\Support\Str;

class ProductController extends Controller
{
     /**
         * Change product stock setting
         *
         * @param $turn
         * @return \Illuminate\Http\RedirectResponse
         *
         */
        public function changeStock($turn): \Illuminate\Http\RedirectResponse
        {
            try{
                $this -> setStock($turn);
                session() -> flash('success', 'You have changed your product stock setting');
            }
            catch (RequestException $e){
                session() -> flash('errormessage', $e -> getMessage());
            }
            return redirect() -> back();
    
        }

我的 User.php 模型:

 <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;
    use Carbon\Carbon;
    use App\Traits\UUIDs;
    use App\Models\Conversation;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, UUIDs;
    
        /**
             * Sets the product stock status
             *
             * @param $turn
             * @throws RequestException
             */
            public function setStock($turn)
            {
                // sets stock value
                $this -> in_stock = $turn == 0;
                $this -> save();
        
            }
    

刀片文件:

      <div class="btn-group btn-group-sm w-100 my-1 mb-4" role="group" aria-label="Basic example">
                    <a href="{{ route('change.stock', 1) }}" class="btn @if 
                    ($product -> in_stock == 1)
                    btn-success btn-sm @else btn-outline-success btn-sm @endif">In Stock</a>
                    <a href="{{ route('change.stock', 0) }}" class="btn @if 
                    ($product -> in_stock == 0)
                    btn-danger btn-sm @else btn-outline-danger btn-sm @endif">Set Out of Stock</a>
                </div>
php laravel laravel-blade
1个回答
0
投票

您需要将 productturn 参数传递给路线 您可能有权访问刀片文件中的 $product 对象

像这样更新你的代码:

<div class="btn-group btn-group-sm w-100 my-1 mb-4" role="group" aria-label="Basic example">
                    <a href="{{ route('change.stock', [$product->id, 1]) }}" class="btn @if 
                    ($product -> in_stock == 1)
                    btn-success btn-sm @else btn-outline-success btn-sm @endif">In Stock</a>
                    <a href="{{ route('change.stock', [$product->id, 0]) }}" class="btn @if 
                    ($product -> in_stock == 0)
                    btn-danger btn-sm @else btn-outline-danger btn-sm @endif">Set Out of Stock</a>
                </div>
© www.soinside.com 2019 - 2024. All rights reserved.