如何在存储方法 laravel 控制器中分解 select 选项 ID 和名称

How to explode select option id and name in store method laravel controller

我想从 select 选项中获取 ID 和名称以插入 table 我如何获得它?

我的观点blade:

<th>
  <select name="itemIdSelected" id="itemNameSelected" class="form-control nameItem" >
     <option value="0" disabled="true" selected="true" >Select Item Name</option>
        @foreach($listItem as $item)
            <option value="{{ $item->item_id }}:{{$item->item_name}}">{{ $item->item_name }}</option>
        @endforeach
  </select>
</th>

我的控制器:

public function storeOrder(Request $request){
    $order = DB::table('order_item_master_model')->insert([
    'product_id' => $request -> itemIdSelected, // will return 15:Ben Top Biru, how do I get 15
    'customer_name' => $request -> customerName,
    'phone' => $request -> phone,
    'address' => $request -> address,
    'item_name' => $request -> itemIdSelected, // will return 15:Ben Top Biru, how do I get Ben Top Biru
    'qty' => $request -> qty,
    'uom' => $request -> uom,
    'item_price' => $request -> prc,
    'total_price' => $request -> totalPrice,
    'status' => $request -> status,
]);
    return redirect('orderProcess');

这是我的错误: Illuminate\Database\QueryException SQLSTATE[01000]:警告:第 1 行 'product_id' 列的 1265 数据被截断(SQL:插入 order_item_master_modelproduct_id , customer_name, phone, address, item_name, qty, uom, item_price, total_price, status) 值 (15:Ben Top Biru, dana, 081233, purwojersey, 15 :Ben Top Biru, 2, 升, Rp. 4000, Rp. 8000, lunas))

如何解决这个错误

product_id 列的类型/或大小似乎与您要插入的数据的类型/或大小不匹配。 检查 product_id 列的类型/或大小。

使用 PHP 函数 explode() 如下所示:

public function storeOrder(Request $request){

    // Explode the ID and Name and put them in array respectively.
    $extracted = explode(":", $request -> itemIdSelected);

    $order = DB::table('order_item_master_model')->insert([
        'product_id' => $extracted[0], // Will return ID. 
        'customer_name' => $request -> customerName,
        'phone' => $request -> phone,
        'address' => $request -> address,
        'item_name' => $extracted[1], // Will return Name.
        'qty' => $request -> qty,
        'uom' => $request -> uom,
        'item_price' => $request -> prc,
        'total_price' => $request -> totalPrice,
        'status' => $request -> status,
    ]);

    return redirect('orderProcess');
}