public function scopeWithPost($query) { $postTableName = (new Forum\Post())->getTable(); $thisTableName = $this->getTable(); return $query->whereExists(function ($query) use($postTableName, $thisTableName) { $query->select(DB::raw(1))->from($postTableName)->whereRaw("{$postTableName}.post_id = {$thisTableName}.post_id"); }); }