简体   繁体   中英

Laravel Array to string conversion error

I want to store data from a form into two tables on database when the radio button value on my view is "new", but the below problem was happened. But if the value is "existing", it's works fine. what wrong with my code ?

Array to string conversion (SQL: insert into customers ( company_name , address , service_id , tc_name , tc_dept , tc_phone , tc_email , bill_name , bill_dept , bill_phone , bill_email , updated_at , created_at ) values (PT Bank ABC, JL Sudirman, 1, Budi, Technical, 0812345678, budi@abc.co.id, Joko, Finance, 08123456789, joko@abc.co.id, 2016-12-14 11:21:26, 2016-12-14 11:21:26))

here my store code

if($request->select_data == 'new'){
        $customer = New Customer;
        $customer->company_name = $request->company_name;
        $customer->address = $request->address;
        $customer->service_id = $request->service_id;
        $customer->tc_name = $request->tc_name;
        $customer->tc_dept = $request->tc_dept;
        $customer->tc_phone = $request->tc_phone;
        $customer->tc_email = $request->tc_email;
        $customer->bill_name = $request->bill_name;
        $customer->bill_dept = $request->bill_dept;
        $customer->bill_phone = $request->bill_phone;
        $customer->bill_email = $request->bill_email;
        $customer->save();

        $salesorder = New SalesOrder;
        $salesorder->pid = $request->pid;
        $salesorder->project_name = $request->project_name;
        $salesorder->customer_id = $request->company_id;
        $salesorder->total = $request->totalfee;
        $salesorder->status = 'submit';
        $salesorder->detail = $request->detail;
        $salesorder->save();
    }else{
        $salesorder = New SalesOrder;
        $salesorder->pid = $request->pid;
        $salesorder->project_name = $request->project_name;
        $salesorder->customer_id = $request->company_id;
        $salesorder->total = $request->totalfee;
        $salesorder->status = 'submit';
        $salesorder->detail = $request->detail;
        $salesorder->save();
        //dd($salesorder);
    }

dd($request->all()); result

array:32 [▼
  "sales_order_id" => "9"
  "select_data" => "new"
  "company_id" => "2"
  "company_name" => "PT Bank ABC"
  "address" => "JL Sudirman"
  "tc_name" => "Budi"
  "tc_dept" => "Technical"
  "tc_phone" => "0812345678"
  "tc_email" => "budi@abc.co.id"
  "bill_name" => "Joko"
  "bill_dept" => "Finance"
  "bill_phone" => "08123456789"
  "bill_email" => "joko@abc.co.id"
  "pid" => "PID002"
  "project_name" => "Implementasi"
  "order_identifier" => array:2 [▶]
  "service_name" => array:2 [▶]
  "service_id" => array:2 [▶]
  "order_type" => array:2 [▶]
  "select_plan" => array:2 [▶]
  "qty" => array:2 [▶]
  "unit_price" => array:2 [▶]
  "total_price" => array:2 [▶]
  "note" => array:2 [▶]
  "emp_name" => array:1 [▶]
  "emp_id" => array:1 [▶]
  "position" => array:1 [▶]
  "position_id" => array:1 [▶]
  "mandays" => array:1 [▶]
  "detail" => "Coba Coba"
  "totalfee" => "3100"    
  "_token" => "uxmXBwJKHWIoXDSFetU4oRgTiTftYEhhdpx4CaPr"
]

radio button html code

<div class="row-fluid select_data">
                        <input name="select_data" id="select_data" type="radio" value="new">
                        <span class="lbl">New</span>
                        <input name="select_data" id="select_data" type="radio" value="existing">
                        <span class="lbl">Existing</span>
                    </div>

The problem is because some of $request properties are arrays. You can't store an array in integer , string etc columns. If you want to store them as arrays, you can serialize them:

$customer->service_id = json_encode(service_id);

And store in text or json columns:

$table->json('service_id');
$table->text('service_id');

尝试更改两个单选按钮的ID,您不能为不同的元素设置相同的ID,否则其行为会有所不同

Try this one

<input name="select_data" name="select_data" type="radio" value="new">
                        <span class="lbl">New</span>
                        <input name="select_data" name="select_data" type="radio" value="existing">
                        <span class="lbl">Existing</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM