Level 13
If you are using Laravel 5.2, you can create a global scope:
class Model extends Eloquent {
public static function newQuery()
{
static::addGlobalScope('join_scope', function($query) {
$query->leftJoin('table2', 'table2.id', '=', 'table1.rel_col_1');
});
parent::boot();
}
}
Then you can override it as follows:
Model::all(); // includes join
Model::withoutGlobalScope('join_scope')->get(); // doesn't include join
1 like