简体   繁体   English

Laravel数组到字符串转换错误

[英]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. 当我的视图上的单选按钮值为“ new”时,我想将表单中的数据存储到数据库的两个表中,但是发生了以下问题。 But if the value is "existing", it's works fine. 但是,如果该值是“ existing”,那么就可以了。 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)) 数组到字符串的转换(SQL:插入customerscompany_nameaddressservice_idtc_nametc_depttc_phonetc_emailbill_namebill_deptbill_phonebill_emailupdated_atcreated_at )值(PT Bank ABC,JL Sudirman,1,Budi,技术,0812345678,budi@abc.co.id,Joko,金融,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 单选按钮html代码

<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. 问题是因为某些$request属性是数组。 You can't store an array in integer , string etc columns. 您不能将数组存储在integerstring等列中。 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: 并存储在textjson列中:

$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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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