0
0

【Laravel】クエリビルダがJOIN済か確認する方法

Posted at

Laravel=5.6

概要

クエリビルダが、特定のテーブルとのJOINを含んでいるかを確認する方法。getQueryメソッドでクエリビルダについての情報を取得し、その中のjoinsプロパティを参照する。

コード例

XXX.php
private function isJoin (
    Builder $query,
    string $tableName
): bool {
    return count(array_filter($query->getQuery()->joins, function ($join) use ($tableName) {
        // $join->tableの値は"テーブル名 as エイリアス"となっているため、explodeでテーブル名だけを抜き出す
        return explode(' ', $join->table)[0] === $tableName;
    }));
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0