[go: up one dir, main page]

0% found this document useful (0 votes)
30 views3 pages

CRUD Laravel

The document defines a controller class with methods to manage students. It includes methods to display a student index view with a list of students and count, create/edit form views, and store/update methods to save student data to the database. Routes are defined to map controller methods to URLs. The index and edit views display student data and include links to create/edit forms.

Uploaded by

achraf10encg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

CRUD Laravel

The document defines a controller class with methods to manage students. It includes methods to display a student index view with a list of students and count, create/edit form views, and store/update methods to save student data to the database. Routes are defined to map controller methods to URLs. The index and edit views display student data and include links to create/edit forms.

Uploaded by

achraf10encg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

class EtudiantController extends Controller

{
public function index () {
$etudiants=Etudiant::all();
$compte=$etudiants->count();
return
view('etudiants.index',compact('etudiants','compte'));
}
public function create () {
return view('etudiants.create');
}
public function edit () {
return view('etudiants.edit');
}
public function store()
{
//sauvegarder un nouveau etudiant
/* $etud=new Etudiant();
$etud->matricule=100;
$etud->nom="Alami";
$etud->note=15.75;
$etud->save(); */
Etudiant::create(
[
'matricule'=>200,
'nom'=>"Tazi",
'note'=>11.35
]
);
}
public function update()
{
//sauvegarder les modifications d'un etudiant
}
}

Route::get('/',[EtudiantController::class,'index'])->name('index');
Route::get('/create',[EtudiantController::class,'create'])-
>name('create');
Route::get('/edit',[EtudiantController::class,'edit'])-
>name('edit');
Route::get('/store',[EtudiantController::class,'store'])-
>name('store');

Index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
<body>
<h1>page d'accueil</h1>
<a href="{{ route('store') }}">create</a>
<h2>la liste des etudiants</h2>
<table>
<thead>
<tr>
<th>ID</th><th>Matricule</th>
<th>Nom</th><th>Note</th>
</tr>
</thead>
<tbody>
@foreach($etudiants as $etud)
<tr>
<td>{{$etud->id}}</td><td>{{$etud->matricule}}</td>
<td>{{$etud->nom}}</td><td>{{$etud->note}}</td>
</tr>
@endforeach
</tbody>
</table>
<h3>il y a {{ $compte }} etudiants</h3>
</body>
</html>
public function edit ($id) {
$etudiant=Etudiant::find($id);
return view('etudiants.edit',compact('etudiant'));
}

Edit.blade.php

<form action="{{ route('store') }}" method="post">


@csrf
<input type="text" name="matricule" value="{{$etudiant-
>matricule}}"> <br>
<input type="text" name="nom" value="{{$etudiant->nom}}"> <br>
<input type="text" name="note" value="{{$etudiant->note}}"> <br>
<button type="submit">Enregistrer</button>
</form>

Index.blade.php

<td colspan="2">
<a href="{{ route('edit',$etud->id) }}">Edit</a> |
<a href="">Delete</a>
</td>

You might also like