【问题标题】:Missing required parameters for [Route: products.show] [URI: products/{product}][Route: products.show] [URI: products/{product}] 缺少必需参数
【发布时间】:2021-06-27 01:50:54
【问题描述】:

请帮助我,我正在使用 Laravel 6 在将我的 html 代码从无序列表更改为表格后,我遇到了这个问题,我检查了视图代码,但我没有看到错误

这是我的索引视图

<table class="table">
            <thead class="simba-thead">
                <tr>
                    <th>Codigo Interno / Codigo de Parte</th>
                    <th>Descripción</th>
                    <th>Percha / Bodega</th>
                    <th>Stock</th>
                    <th>Precio</th>
                    <th>Activo</th>
                    <th>Editar / Eliminar</th>
                </tr>
            </thead>
            <tbody>
                @forelse($products as $product)
                <tr>
                    <td><a href="{{route('products.show',$product)}}">{{$product->internalCode}}</a><br>{{$product->partNumber}}</td>
                    <td>{{$product->description}}</td>
                    <td>{{$product->rack}}<br>{{$product->warehouse}}</td>
                    <td>1</td>
                    <td>{{$product->Price}}</td>
                    <td>{{$product->Active}}</td>
                    <td>edit/delete</td>
                </tr>
                @empty
                    <td>null</td>
                    <td>null</td>
                    <td>null</td>
                    <td>null</td>
                    <td>null</td>
                    <td>null</td>
                    <td>null</td>
                </tr>
                @endforelse
                {{$products->links()}}
            </tbody>
        </table>

我的控制器

class ProductController extends Controller
{
    public function index(){
        return view('products.index',[
            'products'=>Product::latest()->paginate()
        ]);
    }
    public function show(Product $product){
        return view('products.show',[
            'product'=>$product
        ]);
    }
}

我的路线

Route::resource('products','ProductController')->names('products');

【问题讨论】:

    标签: laravel-6


    【解决方案1】:

    问题是 Laravel 默认使用字段名称 'id' 来 id 表中的主键。 我解决了它,将此行添加到我的模型中。

    protected $primaryKey = 'productId';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2022-01-16
      • 2021-03-10
      • 2020-08-17
      相关资源
      最近更新 更多