doufuxing8691 2017-10-28 20:26
浏览 67
已采纳

基于ID插入多个项目 - Laravel

I have two tables products and shipments and the pivot table product_shipment with fields product_id and shipment_no. In the pivot table, there can be a shipment number eg. #4562 that contains 2 or more products. This is something i want to achieve.

Now this is how my codes works. When i select a product hammer, i am able to retrieve the id of hammer. When i select the id of another product screwdrivers, it replaces the id of hammer instead of storing both.

After inserting my data, i expect something of this sought in my pivot table

Products Table

 product_id   name
    1         hammer
    2         screwdriver

Shipments Table

shipment_no    courrier        
 4562          MaerskLine

product_shipment - Pivot

   shipment_no     product_no
     4562             1
     4562             2

This is where my i select my product checkbox

HTML

 <input onclick="return productSelected(this)" type="checkbox"
 id="{!! $product->id !!}" name="{!! $product->name !!}" /> 

How i select and retrieve my product id when i select a checkbox

JS

  function productSelected(product)
{

    $(".panel").append(
    '<input type="text"  value='+product.id+'   " id="product_id"  name="product_id" />'

}

How i save data into my database

Controller

$product_id = Input::get('product_id');
        $product_selected = Product::all()->where('id',$product_id)->first();

        $shipment_details = new Shipment(array(
            'shipment_no' => $request->get('shipment_no'),
            'courrier' => $request->get('courrier'),


        ));

        $product_id->shipments()->save($shipment_details);   

Shipment Model

public function products()
    {
        return $this->belongsToMany('App\Product')
        ->withTimestamps();
    }

Product Model

public function shipments()
    {
        return $this->belongsToMany('App\Shipment')
        ->withTimestamps();
    }

How can i get all the id's of the products i select and also have it look like what i have in my pivot table? Thanks for your help

  • 写回答

1条回答 默认 最新

  • dongshuobei1037 2017-10-28 20:41
    关注

    first change input names like this:

    function productSelected(product)
    {
    
        $(".panel").append(
        '<input type="text"  value='+product.id+'   " id="product'+product.id+'"  
         name="products[]" />'
    
    }
    

    You should get array products with all selected ids in your controller now. So you can do something like that:

    $shipment_details = new Shipment(array(
            'shipment_no' => $request->get('shipment_no'),
            'courrier' => $request->get('courrier'),
    ));
    $shipment_details->save();
    $shipment_details->products()->sync($request->get('products'));
    

    Of course you should have manyToMany relation with Product in your Shipment model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题