public function getAcGoitanthu($server_id, $gift_type) { $user = session('user'); if (User::findOrfail(Auth::user()->id)->fresher) { $server = Server::findOrfail($server_id); $giftFresher = GiftFresher::findOrfail($gift_type); $account = Session::get('account'); $oldKNB = $account->yuanbao; $oldPoint = $account->point; if ($oldKNB >= $giftFresher->KNB && $oldPoint >= $giftFresher->point) { $user = User::findOrfail(Auth::user()->id); $giftBox = new GiftBox(); $giftBox->setConnection($server->user_db); $giftBox->acct_id = session('account')->acct_id; $giftBox->item_id = $giftFresher->item_id; $giftBox->itemtype = 0; $giftBox->quantity = 1; $giftBox->serialNo = 0; try { $update = $giftBox->save(); } catch (\Exception $e) { return redirect()->back()->with('error', 'Thao tác xảy ra lỗi'); } if ($update) { try { $newKNB = $oldKNB - $giftFresher->KNB; $newPoint = $oldPoint - $giftFresher->point; $account->yuanbao = $newKNB; $account->point = $newPoint; $account->save(); $user->update(['fresher' => false]); Session::forget('account'); Session::put('account', $account); return redirect()->back()->with('message', 'Nhận quà thành công!'); } catch (\Exception $e) { return redirect()->back()->with('error', 'Thao tác xảy ra lỗi'); } } } else { return redirect()->back()->with('error', 'Không đủ KNB hoặc điểm tích lũy'); } } else { return redirect()->back()->with('error', 'Quà chỉ dành cho tân game thủ'); } }