首页 > 解决方案 > 如何从 Laravel 的数据库中获取产品 ID

问题描述

我正在尝试使用隐藏输入从数据库中获取产品 ID,但被卡住了;我得到了错误General error: 1366 Incorrect integer value: '[{"id":1}]' for column 'product_id'。如何从数据库中获取产品 ID?

<input type="hidden" name="id" value="" />

控制器

Image::create(array_merge($formInput,
    [
        $id = $request->input('id'),
        $product_id = Product::find('id'),
        'product_id' => $product_id,

    ])); 

更新

这是我更新的控制器。

Image::create(array_merge($formInput,
[
    $id = $request->input('id'),
    $product = Product::get($id),
    'product_id' =>$product->id,

])); 

标签: phpmysqllaravellaravel-5

解决方案


  1. 用户$id而不是'id'
  2. 获取$product对象
  3. 使用 id产品的
  4. 利用::find(id)

所以在你的代码中,改变

  $id=$request->input('id'),
    $product_id=Product::get('id'),
    'product_id' =>$product_id,

  $id=$request->input('id'),
    $product=Product::find($id), /// I assume id is the PK
    'product_id' =>$product->id

推荐阅读