Laravel 数组到字符串转换错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41141625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:55:27  来源:igfitidea点击:

Laravel Array to string conversion error

phpmysqllaravel

提问by rafitio

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, [email protected], Joko, Finance, 08123456789, [email protected], 2016-12-14 11:21:26, 2016-12-14 11:21:26))

数组到字符串的转换(SQL:插入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) 值 (PT Bank ABC, JL Sudirman, 1, Budi, Technical, 0812345678, [email protected], Joko,金融, 08123456789, [email protected], 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

dd($request->all());结果

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" => "[email protected]"
  "bill_name" => "Joko"
  "bill_dept" => "Finance"
  "bill_phone" => "08123456789"
  "bill_email" => "[email protected]"
  "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>

采纳答案by Alexey Mezenin

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

问题是因为某些$request属性是数组。你不能存储在一个阵列integerstring等列。如果要将它们存储为数组,可以序列化它们:

$customer->service_id = json_encode(service_id);

And store in textor jsoncolumns:

并存储在textjson列中:

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

回答by Rahul

Try to change id of both the radio buttons, you can not set same id for different elements, it will behave differently

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

回答by Soniya Reddy Basireddy

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>