0% found this document useful (0 votes)
25 views

TP Laravel

This document describes how to create an MVC structure in Laravel for managing filieres (departments) and stagiaires (students). It involves creating migrations, models, controllers, routes and views.

Uploaded by

Khalid Yn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

TP Laravel

This document describes how to create an MVC structure in Laravel for managing filieres (departments) and stagiaires (students). It involves creating migrations, models, controllers, routes and views.

Uploaded by

Khalid Yn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

TP 0 : MVC

1- Créer la migration
- Table filieres
Schema::create('filieres', function (Blueprint $table) {
$table->string('code')->primary();
$table->string('libelle');
$table->timestamps();
});

- Tables stagiaires

Schema::create('stagiaires', function (Blueprint $table) {


$table->id();
$table->string('nom');
$table->string('prenom');
$table->string('email')->unique();
$table->string('tel');
$table->string('ville');
$table->text('adresse');
$table->date('date_naissance');
$table->string('photo')->nullable();
$table->string('filiere'); // Changed to string data type
$table->foreign('filiere')->references('code')->on('filieres')->onDelete('cascade'); // Adjust
onDelete behavior as needed
$table->timestamps();
});

2- Créer les models


- Filiere

protected $fillable = ['code','libelle'];


public function stagiaires()
{
return $this->hasMany(Stagiaire::class, 'filiere');
}

- Stagiaire

public function filiere()


{
return $this->belongsTo(Filiere::class, 'filiere');
}

3- Créer les controlleurs


- FiliereController

use App\Models\Filiere;
use Illuminate\Http\Request;

class FiliereController extends Controller


{
public function index()
{
$filieres = Filiere::all();
return view('filieres.index', ['filieres' => $filieres]);
}

public function show($code)


{
// $filiere = Filiere::find($id);
$filiere = Filiere::where('code', $code)->first();
if (!$filiere) {
abort(404);
}
return view('filieres.show', ['filiere' => $filiere]);
}

public function create()


{
return view('filieres.create');
}

public function store(Request $request)


{
$validatedData = $request->validate([
'code' => 'required|unique:filieres|max:255',
'libelle' => 'required|max:255'
]);

$filiere = Filiere::create($validatedData);

return redirect()->route('filieres.index')->with('success',
'Filiere créée avec succès.');
}

public function edit($code)


{
//$filiere = Filiere::find($id);
$filiere = Filiere::where('code', $code)->first();
if (!$filiere) {
abort(404);
}
return view('filieres.edit', ['filiere' => $filiere]);
}

public function update(Request $request, $code)


{
$donne = $request->validate([
'code' => 'required|max:255',
'libelle' => 'required|max:255'
]);

// $filiere = Filiere::find($id);
$filiere = Filiere::where('code', $code)->first();
if (!$filiere) {
abort(404);
}

$filiere->update($donne);

return redirect()->route('filieres.index')->with('success',
'Filiere modifiée avec succès.');
}

public function destroy($code)


{
//$filiere = Filiere::find($id);
$filiere = Filiere::where('code', $code)->first();
if (!$filiere) {
abort(404);

} else{
$filiere->delete();
return redirect()->route('filieres.index')->with('success',
'Filiere supprimée avec succès.');
}

}
}
-

- StagiaireController

4- Créer les routes

use App\Http\Controllers\FiliereController;
use App\Http\Controllers\StagiaireController;

Route::resource('filieres', FiliereController::class);
Route::resource('stagiaires', StagiaireController::class);

5- Créer les views


- layouts/app.blade.php (template)

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ config('app.name') }}</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.mi
n.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65Vohhp
uuCOmLASjC" crossorigin="anonymous">
<script src="{{ asset('js/bootstrap.bundle.min.js') }}"></script>
</head>
<body>
<header class="d-flex justify-content-between align-items-center py-
3 bg-white border-bottom">
<a href="{{ url('/') }}" class="container d-flex align-items-
center text-dark text-decoration-none">
<svg xmlns="http://www.w3.org/2000/svg" width="24"
height="24" fill="none" stroke="currentColor" stroke-linecap="round"
stroke-linejoin="round" stroke-width="2" class="mx-2" viewBox="0 0 24
24"><path d="M12 2L2 7l10 5 10-5z"></path><path d="M22 12c-2.21 0-4
1.79-4 4v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2v-5c0-2.21-1.79-4-4-
4z"></path></svg>
<span class="fs-4">{{ config('app.name') }}</span>
</a>
<nav class="d-flex">
<ul class="nav nav-pills">
<li class="nav-item">
<a href="{{ route('filieres.index') }}" class="nav-
link @if(request()->routeIs('filieres.*')) active @endif">Filières</a>
</li>
<li class="nav-item">
<a href="{{ route('stagiaires.index') }}"
class="nav-link @if(request()->routeIs('stagiaires.*')) active
@endif">Stagiaires</a>
</li>
</ul>
@auth
<a href="{{ route('logout') }}" class="btn btn-sm btn-
outline-primary ms-3">Déconnexion</a>
@endauth
</nav>
</header>

<div class="container mt-4">


@yield('content')
</div>

<script src="{{ asset('js/app.js') }}"></script>


</body>
</html>
-

- filieres/index.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
<h1>Liste des filières</h1>

@if (session('success'))
<div class="alert alert-success" role="alert">
{{ session('success') }}
</div>
@endif

<a href="{{ route('filieres.create') }}" class="btn btn-primary


mb-3">Créer une filière</a>

<table class="table table-striped">


<thead>
<tr>
<th>Code</th>
<th>Libelle</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@forelse ($filieres as $fil)
<tr>
<td>{{ $fil->code }}</td>
<td>{{ $fil->libelle }}</td>
<td>
<a href="{{ route('filieres.show', $fil-
>code) }}" class="btn btn-sm btn-info">Voir</a>
<a href="{{ route('filieres.edit', $fil-
>code) }}" class="btn btn-sm btn-warning">Modifier</a>
<form action="{{ route('filieres.destroy',
$fil->code) }}" method="POST" class="d-inline">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm
btn-danger" onclick="return confirm('Êtes-vous sûr de vouloir supprimer
cette filière ?')">Supprimer</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="3">Aucune filière trouvée.</td>
</tr>
@endforelse
</tbody>
</table>
</div>
@endsection
-
- filieres/create.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
<h1>Créer une filière</h1>

@if ($errors->any())
<div class="alert alert-danger">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</div>
@endif

<form action="{{ route('filieres.store') }}" method="POST">


@csrf
<div class="mb-3">
<label for="code" class="form-label">Code</label>
<input type="text" class="form-control" id="code"
name="code" value="{{ old('code') }}">
</div>
<div class="mb-3">
<label for="libelle" class="form-label">Libelle</label>
<input type="text" class="form-control" id="libelle"
name="libelle" value="{{ old('libelle') }}">
</div>
<button type="submit" class="btn btn-primary">Créer</button>
</form>
</div>
@endsection
-

- filieres/edit.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
<h1>Modifier la Filière</h1>

@if ($errors->any())
<div class="alert alert-danger" role="alert">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if ($filiere)
<form method="POST" action="{{ route('filieres.update', $filiere-
>code) }}">
@csrf
@method('PUT') <div class="form-group">
<label for="code">Code:</label>
<input type="text" name="code" id="code" class="form-control"
value="{{ old('code', $filiere->code) }}">
</div>
<div class="form-group">
<label for="libelle">Libellé:</label>
<input type="text" name="libelle" id="libelle" class="form-
control" value="{{ old('libelle', $filiere->libelle) }}">
</div>
<button type="submit" class="btn btn-primary">Modifier</button>
</form>
@else
<p>Filière non trouvée!</p>
@endif

<a href="{{ route('filieres.index') }}" class="btn btn-


secondary">Retour</a>
</div>
@endsection
-

- filieres/show.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
<h1>Détails de la Filière</h1>

@if ($filiere)
<p><b>Code:</b> {{ $filiere->code }}</p>
<p><b>Libellé:</b> {{ $filiere->libelle }}</p>
@else
<p>Filière non trouvée!</p>
@endif

<a href="{{ route('filieres.edit', $filiere->code ?? '') }}" class="btn


btn-primary">Modifier</a>
<a href="{{ route('filieres.index') }}" class="btn btn-
secondary">Retour</a>
</div>
@endsection

You might also like