【问题标题】:Configure Route apiResource in laravel在 laravel 中配置路由 apiResource
【发布时间】:2021-10-31 15:42:58
【问题描述】:

我的控制器中有代码:

    public function show(Postmeta $sku)
{
    $sql = "select * from wp_posts po
    join wp_postmeta wp on wp.post_id = po.ID and wp.meta_key = '_sku'
    where po.post_type = 'product' and wp.meta_value = ?";

    $prod = DB::select($sql, array($sku));

    return response(['product' => CEOResource::collection($prod),
        'message' => 'Retrived Successfully'], 200);
}

我的路线是:

Route::apiResource('productos', ProductosController::class )->parameters([
'productos' => 'sku'])->middleware('auth:api');

我调用 API:

http://localhost:8081/sportzone-api/public/api/productos/woo-belt

结果是:

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `wp_postmeta` where `id` = woo-belt limit 1)",
"exception": "Illuminate\\Database\\QueryException",

我不明白这个问题。

谢谢

【问题讨论】:

  • 该错误似乎与您提供的代码无关。它通知您在您的数据库中的 wp_postmeta 表中不存在列 id
  • 但是查询没有问题,那么问题可能出在 Route::apiResource()

标签: php laravel api


【解决方案1】:

我解决问题改变我的行动:

    public function show($producto)
{
    $match = [
        'wp_postmeta.meta_key' => '_sku',
        'wp_posts.post_type' => 'product',
        'wp_postmeta.meta_value' => $producto
    ];
    
    $prod = Postmeta::join("wp_posts","wp_postmeta.post_id","=","wp_posts.ID")
    ->where($match)
    ->get();   
    

    return response(['product' => CEOResource::collection($prod),
        'message' => 'Retrived Successfully'], 200);
}

并更改 mi 路线:

Route::apiResource('/productos', ProductosController::class);

谢谢

【讨论】:

    猜你喜欢
    • 2020-01-29
    • 2022-11-10
    • 2021-07-31
    • 2015-11-07
    • 1970-01-01
    • 2022-01-02
    • 2013-11-09
    • 2020-02-19
    • 2021-10-30
    相关资源
    最近更新 更多