create.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-4 mx-auto">
<div class="mb-3 text-primary fs-3 text-center">
{{$titre}}
</div>
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{route('produits.store')}}" method="post" >
@csrf
<div class="mb-3">
<label for="libelle" class="form-label text-
capitalize">libelle:</label>
<input required type="text" name="libelle" id="libelle"
class="form-control">
</div>
<div class="mb-3">
<label for="prix" class="form-label text-
capitalize">prix:</label>
<input required type="number" name="prix" id="prix" class="form-
control">
</div>
<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> <div class="col-sm-3">
<div class="mt-5">
<a class="btn btn-primary"
href="http://127.0.0.1:8000/produits/create">creation d'un produit</a>
</div>
</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>
ProduitController.php
<?php
namespace App\Http\Controllers;
use App\Models\Produit;
use Illuminate\Http\Request;
class produitController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$produits=Produit::all();
$titre="liste des produits";
return view("produits/index",compact('produits','titre'));
}
/**
* 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;
class CreateTableProduit extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('produits', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('libelle');
$table->double('prix')->default(0);
$table->timestamps();
});
}
/**
* 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;
class Produit extends 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