public static function inGame($userId, $poolId) { return PoolPlayer::where('user_id', '=', $userId)->where('pool_id', '=', $poolId)->first(); }
public function purchaseSquare($id) { try { $square = PoolSquare::findOrFail($id); // use a transaction to prevent any issues // from concurrent modifications DB::transaction(function () use($square) { $thisSquare = $square->newQuery()->lockForUpdate()->find($square->id, ['id', 'status']); if ($thisSquare->status['id'] == PoolSquare::STATUS_OPEN) { $curUser = Auth::user()->id; $thisSquare->status = PoolSquare::STATUS_PENDING; $thisSquare->user_id = $curUser; $thisSquare->save(); $hasPlayeRecord = PoolPlayer::where('user_id', '=', $curUser)->where('pool_id', '=', $square->pool_id)->count(); if (!$hasPlayeRecord) { $poolCreator = new PoolPlayer(); $poolCreator->user_id = $curUser; $poolCreator->pool_id = $square->pool_id; $poolCreator->pool_admin = 0; $poolCreator->has_paid = 0; $poolCreator->save(); } return response()->json('success'); } else { return response()->json('square not available'); } }); } catch (Exception $e) { return response()->json($e); } }