public function store(Request $request) { $userPlayers = new UserPlayers(); //$team->where('team_name', $request->team_name)->first()->contract_id; $playerContractId = Contracts::where('player_id', $request->player_id)->select('contract_id')->first()->contract_id; $playerClubId = Contracts::where('player_id', $request->player_id)->select('club_id')->first()->club_id; $countPlayers = $userPlayers->join('fantasy_contracts', 'fantasy_user_players.id', '=', 'fantasy_contracts.player_id')->join('fantasy_club', 'fantasy_contracts.club_id', '=', 'fantasy_club.club_id')->where('user_id', $request->user()->id)->where('fantasy_club.club_id', $playerClubId)->count(); $config = Config::select()->first(); if ($userPlayers->where('user_id', $request->user()->id)->where('id', $playerContractId)->first()) { $error = "havePlayer"; } elseif (!($countPlayers < $config->same_team_player)) { $error = "playerLimit"; } elseif ($request->user()->credits - Players::where('player_id', $request->player_id)->select('price')->first()->price <= 0) { $error = "noCredits"; } else { User::where('id', $request->user()->id)->update(['credits' => $request->user()->credits - Players::where('player_id', $request->player_id)->select('price')->first()->price]); $userPlayers->team_id = $request->user()->team->team_id; $userPlayers->user_id = $request->user()->id; $userPlayers->id = $playerContractId; $userPlayers->save(); return redirect()->back(); } //return new RedirectResponse(url('add_player_in_team')); return redirect()->back()->with('error', trans('front/site.' . $error)); }