簡體   English   中英

如何從Laravel中的數據庫獲取產品ID

[英]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,
       ])
); 
  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM