TournamentController.php 1.49 KB
Newer Older
1 2 3 4 5 6
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Game;
quentin.vrel's avatar
quentin.vrel committed
7
use App\Tournament;
8 9 10

class TournamentController extends Controller
{
quentin.vrel's avatar
quentin.vrel committed
11 12 13 14
    public function index($args = []) {
        $games = Game::select('titre', 'id_game')->get();
        $args['games']=$games;
        return view('create-tournament', $args);
15 16
    }

quentin.vrel's avatar
quentin.vrel committed
17
    public function createTournament(Request $request){
18
        
quentin.vrel's avatar
quentin.vrel committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        $tournament = new Tournament();
        $tournament->description=$request->input('description');
        $year= $request->input('year-begin');
        $month= $request->input('month-begin');
        $day= $request->input('day-begin');
        $tournament->tournament_date=$year . '-' . $month . '-' . $day;
        $tournament->id_game=$request->input('select-game');
        $tournament->created_at=date('Y-m-d');
        try {
            $tournament->save();
        } catch (Exception $e) {
            return $this->index(['post'=>false]);
        }
        return $this->index(['post'=>true]);
    }

    public function displayTournaments(){
        $tournaments=Tournament::select('description','tournament_date','id_game')->get();
        $games = Game::select('id_game','titre')->get();
        $gamesTitles = [];
        foreach ($games as $game) {
            $gamesTitles[$game->id_game]=$game->titre;
        }
        foreach ($tournaments as $tournament) {
            $tournament->titre=$gamesTitles[$tournament->id_game];
        }
        return view('home', ['tournaments'=>$tournaments]);
46 47
    }
}