【问题标题】:How to get product Id from database in Laravel如何从 Laravel 的数据库中获取产品 ID
【发布时间】:2019-06-30 11:19:40
【问题描述】:

我正在尝试使用隐藏输入从数据库中获取产品 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,

])); 

【问题讨论】:

  • $product_id=Product::get('id'), 应该是`$product_id=Product::select('id')->get(),`
  • 这也会引发错误Property [id] does not exist on this collection instance.@DhananjayKyada

标签: php mysql laravel laravel-5


【解决方案1】:
  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

【讨论】:

  • 显示错误SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server@Danyal
  • 现在我收到 Trying to get property of non-object 错误来自这一行 'product_id' =&gt;$product-&gt;id, @Danyal
  • 你缺少这部分$product=Product::find($id),
  • @user11710915 正确检查线路,你错过了
  • 你是什么意思我错过了这部分? $product=Product::find($id)@Danyal
猜你喜欢
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-01
  • 2020-04-29
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多