php - 如何从 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,
]));
解决方案
- 用户
$id
而不是'id'
- 获取
$product
对象 - 使用
id
产品的 - 利用
::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
推荐阅读
- java - 使用 URL 访问注册页面时出现问题
- avaloniaui - 是否可以在 Avalonia 中注册服务?
- c# - 如何限制 C# ReadKey 条目?
- python - 从 Python 中的另一个目录加载 JSON 文件
- mysql - 构建 Builder 时抛出了以下断言:在构建期间调用了 setState() 或 markNeedsBuild()?
- cmake - QtCreator 更新后无法运行 CMake
- javascript - Bootstrap 4 开关更改事件将不起作用
- r - 排序数据框并计算列的即时值
- python - django中reverse()中的url名称和视图名称有什么区别?
- python - 如何使用 pybind11 重新加载使用 c++ 源代码构建的 python 模块