Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trouble inserting multiple records into DB w/Laravel and Query Builder

Posted on 2016-09-04
2
Medium Priority
?
375 Views
Last Modified: 2016-09-04
Hi,
I have an array I'd like to insert into a table. I'm  using laravel 5 and  Query Builder.
$saleHours array looks like this:
array:7 [▼
  1 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "21:00:00"
    "department" => 0
    "dow" => 1
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  2 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "21:00:00"
    "department" => 0
    "dow" => 2
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  3 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "21:00:00"
    "department" => 0
    "dow" => 3
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  4 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "21:00:00"
    "department" => 0
    "dow" => 4
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  5 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "18:00:00"
    "department" => 0
    "dow" => 5
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  6 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "17:00:00"
    "department" => 0
    "dow" => 6
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
  7 => array:5 [▼
    "'open'" => "00:00:00"
    "'closed'" => "00:00:00"
    "department" => 0
    "dow" => 7
    "dealership_id" => "21aa5bb3-b667-3cc6-82f3-56abc164c656"
  ]
]

Open in new window


I've tried a few ideas like:

DB::table('dealership_hours')->insert($salesHours);

and then tried drilling down further:

foreach($salesHours as $sales){
DB::table('dealership_hours')->insert($sales);
}

Both with similiar errors. The first attempts errors gives me:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ''closed'' in 'field list' (SQL: insert into `dealership_hours` (`'closed'`, `'open'`, `dealership_id`, `department`, `dow`) values (21:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 1), (21:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 2), (21:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 3), (21:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 4), (18:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 5), (17:00:00, 09:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 6), (00:00:00, 00:00:00, 21aa5bb3-b667-3cc6-82f3-56abc164c656, 0, 7))

Open in new window


And continues like this:
 in Connection.php line 620
at Connection->runQueryCallback('insert into `dealership_hours` (`'closed'`, `'open'`, `dealership_id`, `department`, `dow`) values (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?)', array('21:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '1', '21:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '2', '21:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '3', '21:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '4', '18:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '5', '17:00:00', '09:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '6', '00:00:00', '00:00:00', '21aa5bb3-b667-3cc6-82f3-56abc164c656', '0', '7'), object(Closure)) in Connection.php line 576

Open in new window


Any idea how I should be inserting?
0
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 41783750
array:7 [▼
  1 => array:5 [▼
    "'open'" => "09:00:00"
    "'closed'" => "21:00:00"

must be:

array:7 [▼
  1 => array:5 [▼
    "open" => "09:00:00"
    "closed" => "21:00:00"

from the error message, you can see that it already quotes (escapes) the field names
1
 
LVL 1

Author Comment

by:tjyoung
ID: 41783801
duh... shoot me :)
Thanks for pointing it out!
Hour or two of my life I'll never get back...
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question