1.
php artisan make:migration create_affectations_table
Contenu de la migration :
public function up()
{
Schema::create('affectations', function (Blueprint $table) {
$table->id();
$table->foreignId('employe_id')->constrained()->onDelete('cascade');
$table->foreignId('materiel_id')->constrained()->onDelete('cascade');
$table->date('date_debut');
$table->date('date_fin')->nullable();
$table->timestamps();
});
}
2. Création des modèles :
Commandes :
php artisan make:model Materiel
php artisan make:model Employe
php artisan make:model Departement
C exemple dans Employe.php :
public function departement() {
return $this->belongsTo(Departement::class);
}
public function affectations() {
return $this->hasMany(Affectation::class);
}
3.
php artisan make:controller MaterielController
Contenu du contrôleur :
public function AfficherListeMateriels() {
$materiels = Materiel::all();
return view('index', compact('materiels'));
}
public function AjouterMateriel(Request $request) {
Materiel::create($request->all());
return redirect()->action([MaterielController::class,
'AfficherListeMateriels']);
}
public function SupprimerMateriel($id) {
Materiel::destroy($id);
return redirect()->action([MaterielController::class,
'AfficherListeMateriels']);
}
4.
<table>
<tr>
<th>code</th><th>marque</th><th>description</th><th>date
début</th><th>action</th>
</tr>
@foreach($materiels as $materiel)
<tr>
<td>{{ $materiel->code }}</td>
<td>{{ $materiel->marque }}</td>
<td>{{ $materiel->description }}</td>
<td>{{ $materiel->date_debut_utilisation }}</td>
<td>
<form method="POST" action="{{ route('materiel.supprimer', $materiel-
>id) }}">
@csrf
@method('DELETE')
<button type="submit">supprimer</button>
</form>
</td>
</tr>
@endforeach
</table>
5.
public function AfficherListeEmployes() {
$employes = Employe::with('departement', 'affectations.materiel')->get();
return view('employes', compact('employes'));
}
public function ConsulterDetailsMateriels($id) {
$employe = Employe::with('affectations.materiel')->findOrFail($id);
return view('details', compact('employe'));
}
6.
Route::get('/materiels', [MaterielController::class, 'AfficherListeMateriels'])-
>name('materiel.liste');
Route::post('/materiels/ajouter', [MaterielController::class, 'AjouterMateriel'])-
>name('materiel.ajouter');
Route::delete('/materiels/supprimer/{id}', [MaterielController::class,
'SupprimerMateriel'])->name('materiel.supprimer');
Route::get('/employes', [EmployeController::class, 'AfficherListeEmployes'])-
>name('employe.liste');
Route::get('/employes/{id}/details', [EmployeController::class,
'ConsulterDetailsMateriels'])->name('employe.details');