Fajar
4 months ago
493
4
Laravel

store array

Posted 4 months ago by Fajar

hello how to store array data like this blade

<label for="nama_barang">Nama Barang</label>
                                    <select name="product_id[]" id="" class="form-control" multiple="multiple">
                                        <option value="">Pilih Nama Barang</option>
                                        @forelse($products as $product)
                                            <option value="{{$product->id}}">{{$product->nama_barang}}</option>
                                        @empty
                                            <option value=""> tidak ada pilihan </option>
                                        @endforelse
                                    </select>

controller

$penjualan = Penjualan::create([
            'nama_customer' => $request->input('nama_customer'),
            'product_id'    => json_encode($request->product_id),
            'quantity'       => $request->input('quantity'),

        ]);

migration

$table->unsignedInteger('product_id');
SQLSTATE[HY000]: General error: 1366 
Incorrect integer value: '["1","2"]' 
for column 'product_id' at row 1 (SQL: insert into `penjualans` 
(`nama_customer`, `product_id`, `quantity`, `updated_at`, `created_at`) values (M.iqbal, ["1","2"], 3, 2020-05-14 20:56:05, 2020-05-14 20:56:05))

thx

Please sign in or create an account to participate in this conversation.