Nyxynyx Nyxynyx - 3 months ago 119
PHP Question

INSERT IGNORE using Laravel's Fluent

Is there a quick way to modify a SQL query generated by Laravel's Fluent to have a

INSERT IGNORE
instead of the usual
INSERT
?

I am trying to insert an array with 50 elements and writing out the entire query manually will bloat up the code and make it more susceptible to human errors.

Answer

For the job you need to create a new Grammar that will have the right string in there:

https://github.com/laravel/laravel/blob/4d3c68129b89c6bc8131aca765a63dba8fee92b0/laravel/database/query/grammars/grammar.php#L375 [1]

The grammar is a public property of the DB or in this case Database stored connection. This is not really straight forward, but from the visibility of the properties you should be able to inject your special grammar into the database layer.

I also suggest you bring the issue up with the project, they probably have got a better idea how to make that more flexible for cases like these.


[1] This was a former, to the date of the answer reference. If you see this today, you need to adopt to the Laravel version you use, e.g. https://github.com/laravel/framework/blob/4.0/src/Illuminate/Database/Query/Grammars/Grammar.php#L545 for 4.0, these classes have moved into laravel/framework.