[英]How to get product Id from database in Laravel
我試圖通過使用隱藏的輸入從數據庫中獲取產品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,
]));
當您使用Model::get('column')
樣式時,它將返回模型對象。 不僅是列值。 這樣您就可以達到以下列值:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.