Last active
December 4, 2024 10:13
-
-
Save JesseObrien/7418983 to your computer and use it in GitHub Desktop.
Bind parameters into the SQL query for Laravel ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyModel extends Eloquent { | |
public function getSql() | |
{ | |
$builder = $this->getBuilder(); | |
$sql = $builder->toSql(); | |
foreach($builder->getBindings() as $binding) | |
{ | |
$value = is_numeric($binding) ? $binding : "'".$binding."'"; | |
$sql = preg_replace('/\?/', $value, $sql, 1); | |
} | |
return $sql; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slight variation for Laravel 9: