douchen9569 2016-12-14 11:37
浏览 851
已采纳

Laravel Array转换为字符串转换错误

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>
  • 写回答

3条回答 默认 最新

  • douxiu6835 2016-12-14 12:07
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题