HTML
HTML
php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/
bootstrap.min.css" rel="stylesheet" integrity="sha384-
KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-4 mx-auto">
<div class="mb-3 text-primary fs-3 text-center">
{{$titre}}
</div>
@if($errors->any())
<div class="mb-3">
<button class="btn btn-primary">ajouter le produit</button>
</div>
</form>
</div>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle
.min.js" integrity="sha384-
ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe"
crossorigin="anonymous"></script>
</body>
</html>
index.blade.php :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/
bootstrap.min.css" rel="stylesheet" integrity="sha384-
KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-3 mx-auto">
<div class="mb-3 text-primary fs-3">
{{$titre}}
</div>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">libelle</th>
<th scope="col">prix</th>
</tr>
</thead>
<tbody>
@foreach($produits as $p)
<tr>
<th scope="row">{{$p->id}}</th>
<td>{{$p->libelle}}</td>
<td>{{$p->prix}}</td>
</tr>
@endforeach
</table>
</tbody>
</div>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle
.min.js" integrity="sha384-
ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe"
crossorigin="anonymous"></script>
</body>
</html>
ProduitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Produit;
use Illuminate\Http\Request;
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$titre="nouveau produit";
return view("produits/create",compact('titre'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'libelle'=>'required|unique:produits|min:1',
'prix'=>'required|numeric'
]);
$data=$request->all();
$produit = new Produit($data);
$produit->save();
return $this->index();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$produit=Produit::find($id);
$titre="detail du produit : ".$produit->libelle;
return view("produits.show",compact('produit','title'));
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$produit=Produit::find($id);
$titre="detail du produit ".$produit->libelle;
return view("produits.edit",compact('produit','title'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$produit=Produit::find($id);
$produit->update($request->all());
return $this->index();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$produit=Produit::find($id);
$produit->delete();
return $this->index();
}
}
2023-04-27_111712_create-table-produit.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('produits');
}
}
Produit.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use HasFactory;
protected $fillable=["id","libelle","prix"];
}
Web.php
<?php
use App\Http\Controllers\produitController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These