Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

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

I want to get id and name from select option for insert in table how do I get that?

My view 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>

My controller :

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');

Here is my error : IlluminateDatabaseQueryException SQLSTATE[01000]: Warning: 1265 Data truncated for column 'product_id' at row 1 (SQL: insert into order_item_master_model (product_id, customer_name, phone, address, item_name, qty, uom, item_price, total_price, status) values (15:Ben Top Biru, dana, 081233, purwojersey, 15:Ben Top Biru, 2, Liter, Rp. 4000, Rp. 8000, lunas))

How do I solve this error

question from:https://stackoverflow.com/questions/65887491/how-to-explode-select-option-id-and-name-in-store-method-laravel-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use PHP function explode() like below:

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');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...