Either don't use the single quotes around the table name, or use backtick `
$upload = DB::raw("LOAD DATA INFILE '$file' INTO TABLE `$request->table` FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi im trying to use this query
LOAD DATA INFILE 'c:/tmp/discounts.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
in laravel as raw query but that line terminating character actually takes the query on new line and it does not work .
$upload = DB::raw("LOAD DATA INFILE '$file' INTO TABLE '$request->table' FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");
this is the error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''teachertest' FIELDS TERMINATED by ',' LINES TERMINATED BY ' ' IGNORE 1 ROWS' at line 1
I suggest writing a helper function to get around that...something like this:
protected function loadDataFile(string $query)
{
if (config(database.default) != 'mysql') return;
$pdo = DB::connection()->getPdo();
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
$statement = $pdo->prepare($query,[\PDO::ATTR_CURSOR=>\PDO::CURSOR_SCROLL]);
$exec = $statement->execute();
if (!$exec) return $pdo->errorInfo();
return $statement;
}
Then call it ($this assumes method in same class):
$this->loadDataFile("LOAD DATA INFILE '$file' INTO TABLE `$request->table` FIELDS TERMINATED by ',' LINES TERMINATED BY '\n' IGNORE 1 ROWS");
Please or to participate in this conversation.