Skip to content
Snippets Groups Projects
Commit d1cad5e0 authored by OlivierFrancois's avatar OlivierFrancois
Browse files

belongs to many

parent 86f7e2f7
No related merge requests found
......@@ -3,8 +3,10 @@
namespace App\Http\Controllers;
use App\Models\Champion;
use App\Models\Role;
use Illuminate\Http\Request;
class ChampionController extends Controller
{
/**
......@@ -14,7 +16,10 @@ class ChampionController extends Controller
*/
public function index()
{
return view('champion.index', ['champions' => Champion::all()]);
return view('champion.index', [
'champions' => Champion::all(),
'roles' => Role::all()
]);
}
/**
......@@ -37,12 +42,14 @@ class ChampionController extends Controller
{
$data = $request->validate([
'name' => 'string|required',
'level' => 'integer|required'
'level' => 'integer|required',
'role.*' => 'exists:roles,id'
]);
$champion = new Champion;
$champion->fill($data);
$champion->save();
$champion->roles()->attach($data['role']);
return redirect()->route('champion.show', $champion);
}
......@@ -55,7 +62,10 @@ class ChampionController extends Controller
*/
public function show(Champion $champion)
{
return view('champion.show', ['champion' => $champion]);
return view('champion.show', [
'champion' => $champion,
'roles' => Role::all()
]);
}
/**
......@@ -80,11 +90,13 @@ class ChampionController extends Controller
{
$data = $request->validate([
'name' => 'string|required',
'level' => 'integer|required'
'level' => 'integer|required',
'role.*' => 'exists:roles,id'
]);
$champion->fill($data);
$champion->save();
$champion->roles()->sync($data['role']);
return redirect()->route('champion.show', $champion);
}
......
......@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Role;
use App\Models\Champion;
use Illuminate\Http\Request;
class RoleController extends Controller
......
......@@ -12,4 +12,8 @@ class Champion extends Model
use SoftDeletes;
protected $fillable = ['name', 'level'];
public function roles() {
return $this->belongsToMany(Role::class);
}
}
......@@ -12,4 +12,8 @@ class Role extends Model
use SoftDeletes;
protected $fillable = ['label'];
public function champions() {
return $this->belongsToMany(Roles::class);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateChampionRoleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('champion_role', function (Blueprint $table) {
$table->foreignId('champion_id');
$table->foreignId('role_id');
$table->primary(['champion_id', 'role_id']);
$table->foreign('champion_id')->references('id')->on('champions')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onUpdate('cascade')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('champion_role');
}
}
......@@ -21,6 +21,16 @@
<label for="level">Level</label> <br>
<input type="number" name="level" value="{{ old ('level') }}">
</div>
<div>
@foreach ($roles as $role)
<input type="checkbox" name="role[]" value="{{ $role->id }}"
@if(in_array($role->id, old('role',[])))
checked
@endif
> {{ $role->label}} <br>
@endforeach
</div>
<button type="submit">Créer le champion</button>
</form>
......
......@@ -9,18 +9,25 @@
<h2>Editer le champion</h2>
<form action=" {{ route('champion.update', $champion) }}" method="post" class="p-2">
@csrf
@method('PUT');
@method('PUT')
<div>
<label for="name">Nom du champion</label> <br>
<input type="text" name="name" placeholder="Nom du champion" value="{{ old('name', $champion->name) }}">
</div>
<div>
<div>
<label for="level">Level</label> <br>
<input type="number" name="level" value="{{ old('level', $champion->level) }}">
</div>
@foreach ($roles as $role)
<input type="checkbox" name="role[]" value="{{ $role->id }}"
@if(in_array($role->id, old('role', $champion->roles->pluck('id')->all())))
checked
@endif> {{ $role->label}} <br>
@endforeach
<button type="submit">Editer le champion</button>
</form>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment