Skip to content

Instantly share code, notes, and snippets.

@goranprijic
Created December 17, 2014 10:12
Show Gist options
  • Save goranprijic/c578acb0086e8cd85179 to your computer and use it in GitHub Desktop.
Save goranprijic/c578acb0086e8cd85179 to your computer and use it in GitHub Desktop.
Check if table is already joined in Laravel Query Builder
<?php
class BaseModel extends Eloquent {
public static function isJoined($query, $table)
{
$joins = $query->getQuery()->joins;
if($joins == null) {
return false;
}
foreach ($joins as $join) {
if ($join->table == $table) {
return true;
}
}
return false;
}
}
@AeonFr
Copy link

AeonFr commented Mar 26, 2019

Thanks for this snippet, very useful

This method detects all kinds of joins (leftJoin(), join(), etc.)

Clarifying just in case someone else was wondering!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment