在Laravel 5.2中统一/加入数据库和Eloquent的模型查询

I have to check if an user is online (in-game), and if he's not then do another checks to perform a sql update.

Actually, I have these two queries.

$isOnline = DB::connection('mssql')->table('USER_STAT')->where('user_id', $userID)->value('ConnectionStat');
$character = Character::where('acc_id', $userID)->where('Name', $characterName)->firstOrFail();

Is there a way I can use union or join in order to save a DB query? I believe this is not a good practice, and some better method is out there, which I can't find in the docs.

I tried something like this, but without success:

$character = Character::where('acc_id', $userID)->where('Name', $characterName)->firstOrFail();
$result = DB::connection('mssql')->table('USER_STAT')->where('user_id', $userID)->union($character)->get();

Thanks in advance!

Using a relation in your Character Model to the Model of the USER_STAT table you can easily lookup the things you need to determine if an update somewhere is needed.

Eloquent Relationships

This does not consolidate the queries into one, like with a join, but they are not heavy anyway, and using the ORM to the fullest will have more significant advantages in the long run.

If you think you must use an SQL-join you must stick to the querybuilder, but the ORM is usually the better choice

Query Builder Joins