Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 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