[英]How to get product Id from database in Laravel
I'm trying to get the product Id from the database by using the hidden input but am stuck; 我试图通过使用隐藏的输入从数据库中获取产品ID,但是卡住了; I'm getting the error General error: 1366 Incorrect integer value: '[{"id":1}]' for column 'product_id'
. 我收到错误General error: 1366 Incorrect integer value: '[{"id":1}]' for column 'product_id'
。 How do I get the product Id from the database? 如何从数据库中获取产品ID?
Blade 刀
<input type="hidden" name="id" value="" />
Controller 控制者
Image::create(array_merge($formInput,
[
$id = $request->input('id'),
$product_id = Product::find('id'),
'product_id' => $product_id,
]));
Updated 更新
This is my updated controller. 这是我更新的控制器。
Image::create(array_merge($formInput,
[
$id = $request->input('id'),
$product = Product::get($id),
'product_id' =>$product->id,
]));
When you use Model::get('column')
style, it returns model object. 当您使用Model::get('column')
样式时,它将返回模型对象。 Not only column value. 不仅是列值。 So you reach column value like this: 这样您就可以达到以下列值:
Image::create(
array_merge(
$formInput,
[
$id=$request->input('id'),
$product = Product::get($id),
'product_id' => $product->id,
])
);
$id
instead of 'id'
用户$id
而不是'id'
$product
object 获得$product
对象 id
of the product 使用产品的id
::find(id)
使用::find(id)
So in your code, change 因此,在您的代码中,进行更改
$id=$request->input('id'),
$product_id=Product::get('id'),
'product_id' =>$product_id,
to 至
$id=$request->input('id'),
$product=Product::find($id), /// I assume id is the PK
'product_id' =>$product->id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.