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;
}));
}