# Laravel toSql パラメータ付きで出力

Laravel 文字列ヘルパー preg_replace_array 使ってバインドデータと sql 文を合体させる

$sql = preg_replace_array('/\?/', $query->getBindings(), $query->toSql());

パラメータが文字列の場合、'' シングルクォーテーションで括弧ってあげたほうが、SQL 文そのままコピペで使えるので

function brackets($n){
  return "'".$n."'";
}

$sql = preg_replace_array('/\?/', array_map('brackets',$query->getBindings()), $query->toSql());

# $query->getBindings()

getBindings() はLaravel framework query builder 3220 行目あたりにある (opens new window)現在クエリにバインドしたデータを配列として取得できる関数です。

# $query->toSql()

toSql() はLaravel framework query builder 2317 行目あたりにある (opens new window)現在実行のクエリ SQL 文を返してくれます。

# preg_replace_array

preg_replace_array は laravel の文字列ヘルパー関数で、指定パターンを順番に配列中の値に置き換えてくれます。
第 3 引数の文字列に対して第一引数の条件にマッチする箇所を第二引数の配列を順に適用していきます。

# 文字列ヘルパー

Laravel 8 の文字列ヘルパーはたくさんありますが、一部抜粋

use Illuminate\Support\Str;

/**
 * Str::after
 * 指定値に続く文字列をすべて返します。文字列中に指定値が存在しない場合は、文字列全体を返します。
**/
$slice = Str::after('This is my name', 'This is');
// ' my name'

/**
 * Str::before
 * 文字列中の指定値より前の文字列を全部返し
**/
$slice = Str::before('This is my name', 'my name');
// 'This is '

/**
 * Str::camel
 * 文字列をキャメルケース(camelCase)へ変換
**/
$converted = Str::camel('foo_bar');
// fooBar

/**
 * Str::contains
 * 指定文字列に指定値が含まれているかどうかを判別
**/
$contains = Str::contains('This is my name', 'my');
// true
$contains = Str::contains('This is my name', ['my', 'foo']);
// true

/**
 * Str::containsAll
 * 指定文字列に指定配列のすべての値が含まれているかどうかを判別
**/
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
// true

/**
 * Str::endsWith
 * 最初の文字列が2つ目の引数の文字列で終わっているか判定
**/
$result = Str::endsWith('This is my name', 'name');
// true

/**
 * Str::isUuid
 * 指定した文字列が有効なUUIDであることを判定
**/
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
// true

$isUuid = Str::isUuid('laravel');
// false

/**
 * Str::length
 * 指定文字列の長さを返します
**/
$length = Str::length('Laravel');
// 7

/**
 * Str::limit
 * 指定文字列を指定する長さへ切り捨て
 * メソッドに3番目の引数を渡し、切り捨てる文字列の末尾へ追加する文字列を変更できます。
**/
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox...

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// The quick brown fox (...)

/**
 * Str::lower
 * 指定文字列を小文字に変換
**/
/**
 * Str::markdown
 * GitHub風なマークダウンをHTMLに変換
**/
$html = Str::markdown('# Laravel');
// <h1>Laravel</h1>

$html = Str::markdown('# Taylor <b>Otwell</b>', [
    'html_input' => 'strip',
]);
// <h1>Taylor Otwell</h1>

/**
 * Str::padBoth
 * 最後の文字列が目的の長さに達するまで、文字列の両側を別の文字列でパディング
**/
$padded = Str::padBoth('James', 10, '_');
// '__James___'

$padded = Str::padBoth('James', 10);
// '  James   '

/**
 * Str::padLeft
 * 文字列の左側を別の文字列でパディング
 *
 * Str::padRight
 * 文字列の右側を別の文字列でパディング
**/
$padded = Str::padLeft('James', 10, '-=');
// '-=-=-James'

$padded = Str::padLeft('James', 10);
// '     James'

/**
 * Str::remove
 * 文字列から指定する値または値の配列を削除
**/
$string = 'Peter Piper picked a peck of pickled peppers.';
$removed = Str::remove('e', $string);
// Ptr Pipr pickd a pck of pickld ppprs.

/**
 * Str::replace
 * 文字列内の指定した文字列を置き換え
**/
$string = 'Laravel 8.x';
$replaced = Str::replace('8.x', '9.x', $string);
// Laravel 9.x

/**
 * Str::uuid
 * UUID(バージョン4)を生成
**/
return (string) Str::uuid();

/**
 * Str::basename
 * 文字列の最後の名前部分を返します
**/
$string = Str::of('/foo/bar/baz')->basename();
// 'baz'

$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
// 'baz'

# 参考

2021-05-21

同じタグを持つ記事をピックアップしました。