?
Solved

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

Posted on 2016-09-04
2
Medium Priority
?
420 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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
Course of the Month14 days, 16 hours left to enroll

840 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