Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Array to string conversion error

Tags:

php

mysql

laravel

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

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" => "[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

<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>
like image 225
rafitio Avatar asked Aug 24 '26 16:08

rafitio


1 Answers

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');
like image 168
Alexey Mezenin Avatar answered Aug 27 '26 05:08

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!