Avatar of tjyoung
tjyoung

asked on 

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

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?
LaravelPHPMySQL Server

Avatar of undefined
Last Comment
tjyoung
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of tjyoung
tjyoung

ASKER

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

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo