0
votes

I'm trying to copy a row from a table to another table with Laravel Query and I get the following error.

$invoice = Capsule::table('tblinvoices')->where('id', $invoiceid)->get(); //array
$copiedInvoiceid = Capsule::table('mod_myinvoices')->insertGetId(array($invoice));

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: )

I've used CREATE TABLE mod_myinvoices LIKE tblinvoices to create mod_myinvoices table.

Any suggestions ?

2
show code insertGetId()buildok
But $invoice this is an indexed array, like ['0' => Capsule_row_1, ..]. You need to get an associative array like [column_name => value,..]buildok

2 Answers

1
votes

Try it!

Change

    $invoice = Capsule::table('invoices')->where('id', $invoiceid)->get(); 
    $copiedInvoiceid = Capsule::table('myinvoices')->insertGetId(array($invoice));

To

    $invoice = Capsule::table('invoices')->where('id', $invoiceid)->first(); 
    $copiedInvoiceid = Capsule::table('myinvoices')->insertGetId((array)$invoice);
0
votes

Try it.

$invoice = Capsule::table('invoices')->where('id', $invoiceid)->get();

$invoiceArr = (array) $invoice[0]; // convert object to array    

$copiedInvoiceid = Capsule::table('myinvoices')->insertGetId($invoiceArr);