Solved

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

Posted on 2016-09-04
2
57 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now