Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $attributes = ['name' => 'John Doe'];
- $query = User::where('email', 'john@example.com'); // $query->toSql() --> select * from `users` where `email` = ?
- $grammar = $query->getQuery()->getGrammar();
- $sql = $grammar->compileUpdate($query->getQuery(), $attributes); // update `users` set `name` = ? where `email` = ?
- $bindings = $query->getBindings(); // ['john@example.com']
- if( ! array_has($bindings, 'join') ) {
- array_set($bindings, 'join', []); // because Grammar stupidly assumes a join binding
- }
- $bindings = $grammar->prepareBindingsForUpdate($bindings, $attributes); // ['John Doe', 'john@example.com']
- $params = array_values(array_filter($bindings, function ($binding) {
- return ! $binding instanceof \Illuminate\Database\Query\Expression;
- }));
- $replacements = [];
- foreach($params as $i => $param) {
- $sql = substr_replace($sql, '$'.($i+1), stripos($sql, '?'), 1);
- }
- // $sql === update `users` set `name` = $1 where `email` = $2
- $config = config('database.connections.pgsql');
- $connection = pg_connect(
- sprintf('host=%s port=%d dbname=%s user=%s password=%s',
- array_get($config, 'host'),
- array_get($config, 'port'),
- array_get($config, 'database'),
- array_get($config, 'username'),
- array_get($config, 'password')
- ));
- if(! pg_send_query_params($connection, $sql, $params) ) {
- throw new \Exception(pg_last_error($connection));
- }
- pg_close($connection);
- // You could use DB::extend('pgsqlbg') to create a custom driver for `Connection`
- // that accepts `update(string|Query|Model $query, array $bindings = [])` which
- // would mask away the statement preparation details inside of the driver.
- // Unfortunately there will be a lot of PDO assumptions which will need to be nooped.
Add Comment
Please, Sign In to add comment