tippspiel/pfosten/app/Http/Controllers/RoundsController.php

121 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Season;
use App\Round;
use App\Game;
use DB;
class RoundsController extends Controller
{
public function index() {
$activeSeason=Season::getCurrent();
$rounds =null;
//$rounds=Round::all();
if ($activeSeason) {
$activeSeasonId = Season::getCurrent()->id;
$rounds = Round::all()->where('season_id', '=', $activeSeasonId);
}
return view('rounds.index', compact('rounds','activeSeason'));
//return view('rounds.index');
//return view ('welcome');
}
public function create() {
$currentSeason = Season::getCurrent();
if ($currentSeason) {
return view('rounds.create', compact('currentSeason'));
}
redirect()->home()->withErrors('Es muss zuerst eine Saison angelegt werden');
}
public function store() {
// Validate form
$this->validate(request(), [
'name' => 'required',
'game1_team1' => 'required',
'game1_team2' => 'required',
'game1_kickoff' => 'required',
'game2_team1' => 'required',
'game2_team2' => 'required',
'game2_kickoff' => 'required',
'game3_team1' => 'required',
'game3_team2' => 'required',
'game3_kickoff' => 'required',
'game4_team1' => 'required',
'game4_team2' => 'required',
'game4_kickoff' => 'required'
]);
$seasonId = Season::getCurrent()->id;
$round = Round::create([
'name' => request('name'),
'season_id' => $seasonId
]);
$round->save();
$round_id = $round->id;
$game1 = Game::create([
'round_id' => $round_id,
'team1' => request('game1_team1'),
'team2' => request('game1_team2'),
'kickoff' => request('game1_kickoff')
]);
$game2 = Game::create([
'round_id' => $round_id,
'team1' => request('game2_team1'),
'team2' => request('game2_team2'),
'kickoff' => request('game2_kickoff')
]);
$game3 = Game::create([
'round_id' => $round_id,
'team1' => request('game3_team1'),
'team2' => request('game3_team2'),
'kickoff' => request('game3_kickoff')
]);
$game4 = Game::create([
'round_id' => $round_id,
'team1' => request('game4_team1'),
'team2' => request('game4_team2'),
'kickoff' => request('game4_kickoff')
]);
// Sign them in
// Redirect to the home page
return redirect()->home();
}
public function editname($round_id)
{
$round = Round::where('id', $round_id)->first();
return view('rounds.editname', compact('round'));
}
public function storename()
{
// Validate form
$this->validate(request(), [
'round_id' => 'required',
'name' => 'required'
]);
$round = Round::where('id', request('round_id'))->first();
$round->name = request('name');
$round->save();
return redirect('/rounds');
}
}