简体   繁体   English

如何从Laravel中的数据库获取产品ID

[英]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,
       ])
); 
  1. User $id instead of 'id' 用户$id而不是'id'
  2. get $product object 获得$product对象
  3. Use the id of the product 使用产品的id
  4. Use ::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.

相关问题 如何从隐藏字段 Laravel 获取产品 ID - How to get product Id from hidden field Laravel Laravel,如果将商品ID存储到购物车中,如何从商品表中获取商品名称 - Laravel, How to get product name from product table if the product id stored into Cart Laravel从集合中获取产品ID - Laravel get product id from collection 如何使用 laravel 6 中的 session 从数据库中获取登录的用户 ID - How to get logged user id from database using session in laravel 6 Laravel 8 - 如何在 ImagesController 商店中获取产品 ID function - Laravel 8 - How to get product id in ImagesController store function 如何从woocommerce中的链接产品中获取分组的产品ID - How to get the grouped product ID from a linked product in woocommerce 如何从数据库表中获取MySQL和MySQL中的MAX product_id? - How can I get the MAX product_id in MySQL & PHP from database table? 如何在 Laravel 的数据库中插入最后一个 ID? - How to get last id inserted on a database in Laravel? 如何获取ID并从Laravel 5.1中的选项值将ID更新到数据库中 - How to get the id & update the id into database from option value in laravel 5.1 如何从数据库中获取“产品”的多个值? - How to get multiple value for `Product` from Database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM