To get around this, you can use the query log. DB::table('users')->where('id', '1')->update()->toSql() Įrror Call to a member function toSql() on int. If you try to print or dump a SQL statement on an update query, it will throw an error as query has already executed and it returns the number of rows that were affected by the update query. Select * from `users` where `id` = ? // vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3788Īrray:1 [ // vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3788 You can use dump method on it instead to get the query with bindings as well: DB::table('users')->where('id', '1')->dump() There is a ? in there in the query because the values are parameterized. If you do that and want to get the raw SQL query, you can use the toSql method: DB::table('users')->where('id', '1')->toSql() Upon reading the documentation, you're supposed to run the query this way, for queries not returning any valuesĭB::statement('UPDATE table SET something = 1 WHERE id = 1') Īlso, I had used backticks `` for the original longer SQL statement so that seemed to have caused on issue.Īlternatively, I could do it without using eloquent models like: DB::table('users')->where('id', '1')->update() ValueError: PDO::prepare(): Argument #1 ($query) cannot be empty A really small mistake - I wanted to run a MySQL update statement on laravel (no bindings, no parameters, no models) so I wrote this DB::update(`UPDATE table SET something = 1 WHERE id = 1`)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |