Skip to content

Commit

Permalink
Ajout de personnage OK
Browse files Browse the repository at this point in the history
Création du controller Persos
  • Loading branch information
Asterodeia committed Oct 14, 2016
1 parent 0ac722a commit 7e96fb5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 58 deletions.
98 changes: 98 additions & 0 deletions app/Http/Controllers/PersoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

use App\Http\Requests;
use App\Perso;


class PersoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('persos', [
'persos' => Perso::orderBy('created_at', 'asc')->get()
]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$perso = new Perso;
$perso->user_id = 1;
$perso->name = $request->input('name');
$perso->race = $request->input('race');

Log::info('Ajout du perso', ['perso' => $perso]);
$perso->save();
return redirect('/persos');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
52 changes: 26 additions & 26 deletions resources/views/persos.blade.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
@extends('layouts.app')

@section('content')
<div class="row">
<form class="col s12" action="{{url('perso')}}" method="POST">
{{ csrf_field() }}
<h2>Nouveau personnage</h2>
<div class="row">
<div class="input-field col s6">
<input id="name" type="text" class="validate">
<label for="name">Nom</label>
</div>
<div class="input-field col s6">
<input id="race" type="text" class="validate">
<label for="race">Race</label>
</div>
<form class="col s12" action="{{url('persos')}}" method="POST">
{{ csrf_field() }}
<h2>Nouveau personnage</h2>
<div class="row">
<div class="input-field col s6">
<input id="perso-name" name="name" type="text" class="validate">
<label for="perso-name">Nom</label>
</div>
<div class="input-field col s6">
<input id="perso-race" name="race" type="text" class="validate">
<label for="perso-race">Race</label>
</div>
</div>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Créer
<i class="material-icons right">send</i>
</button>
</div>
</form>
</div>
<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Créer
<i class="material-icons right">send</i>
</button>
</div>
</form>
</div>

@if (count($persos) > 0)
<div class="collection">
@foreach ($persos as $perso)
<li class="collection-item">{{ $perso->name }} ({{ $perso->race }})</li>
@endforeach
</div>
@endif
@if (count($persos) > 0)
<div class="collection">
@foreach ($persos as $perso)
<li class="collection-item">{{ $perso->name }} ({{ $perso->race }})</li>
@endforeach
</div>
@endif
@endsection
34 changes: 2 additions & 32 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,7 @@
| to using a Closure or controller method. Build something great!
|
*/
use App\Perso;
use Illuminate\Http\Request;
use App\Http\Controllers\PersoController;

Route::get('/', function () {
return view('persos', [
'persos' => Perso::orderBy('created_at', 'asc')->get()
]);
});

Route::post('/perso', function (Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);

if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}

$perso = new Perso;
$perso->user_id = 1;
$perso->name = $request->name;
$perso->race = $request->race;

return redirect('/');

});

Route::delete('/perso/{id}', function ($id) {
Perso::findOrFail($id)->delete();

return redirect('/');
});
Route::resource('persos', 'PersoController');

0 comments on commit 7e96fb5

Please sign in to comment.