Laravel
Models Migration Relation
Introduction,istallation Strucutre,model,migration Migration,Models,Relation
Les Relations
BelongsTo HasOne HasMany BelongsToMany HasManyThrough
Exemples des Relations
Relations:oneToMany,ManyToMany... Relations:Exemples
Exercices
Exercice 1 Exercice 2
Controllers Views Routes
Routes,Controller,Model,view CRUD: Etudiant CRUD: Car CRUD,Recherche: Book
Validation
Exemple :Projets
Api:Laravel +React
Middleware

Seeders & Factories

Authenfication
Layouts





Car:CRUD

1. Migration

Créez la migration pour le modèle Car avec les champs nécessaires. Utilisez la commande artisan :
php artisan make:migration create_cars_table
Modifiez le fichier de migration dans le dossier database/migrations pour ressembler à ceci :
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->id();
            $table->string('matricule');
            $table->string('marque');
            $table->string('model');
            $table->string('type');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('cars');
    }
}
Exécutez ensuite la migration pour créer la table dans la base de données :
php artisan migrate

2. Model

Créez le modèle Car avec la commande artisan :
php artisan make:model Car
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Car extends Model
{
    protected $table = 'cars'; 
    protected $primaryKey = 'id'; 
    protected $fillable = ['matricule', 'marque', 'model', 'type'];

}

3. Controller

Créez le contrôleur CarController avec les méthodes CRUD :
php artisan make:controller CarController
Modifiez le fichier CarController.php dans le dossier app/Http/Controllers comme suit :
namespace App\Http\Controllers;
use App\Models\Car;

use Illuminate\Http\Request;

class CarController extends Controller
{
    public function ajouter()
    {
        return view('ajouterCar');
    }

    public function sauvegarder(Request $request)
    {
        Car::create($request->all());
        return redirect()->route("Route_listeCars");
    }

    public function lister()
    {
        $cars = Car::all();
        return view('ListerCars', compact('cars'));
    }

    public function detail($id)
    {
        $car = Car::find($id);
        return view('DetailCar', compact('car'));
    }


    public function modifier($id)
    {
        $car = Car::find($id);
        return view('ModifierCars', compact('car'));
    }

    public function mettreAJour(Request $request, $id)
    {
        $car=Car::find($id);
        $car->update($request->all());
       return redirect()->route("Route_listeCars");
    }


    public function supprimer($id)
    {
        $car=Car::find($id);
        $car->delete();   
        return redirect()->route("Route_listeCars");

     }

}

4. Vues

1. resources/views/ajouterCar.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Ajouter une voiture</title>
</head>
<body>

<h2>Ajouter une voiture</h2>

<form action="/sauvegarder-car" method="post">
    @csrf
    <label for="matricule">Matricule :</label>
    <input type="text" name="matricule" required><br>

    <label for="marque">Marque :</label>
    <input type="text" name="marque" required><br>

    <label for="model">Modèle :</label>
    <input type="text" name="model" required><br>

    <label for="type">Type :</label>
    <input type="text" name="type" required><br>

    <button type="submit">Ajouter</button>
</form>

</body>
</html>
2. resources/views/ListerCars.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Liste des voitures</title>
</head>
<body>

<h2>Liste des voitures</h2>

<table border="1">
    <tr>
        <th>Matricule</th>
        <th>Marque</th>
        <th>Modèle</th>
        <th>Type</th>
        <th>Action</th>
    </tr>

    @foreach($cars as $car)
    <tr>
        <td>{{ $car->matricule }}</td>
        <td>{{ $car->marque }}</td>
        <td>{{ $car->model }}</td>
        <td>{{ $car->type }}</td>
        <td>
            <a href="/detail-car/{{ $car->id }}">Détails</a>
            <a href="/modifier-car/{{ $car->id }}">Modifier</a>
            <a href="/supprimer-car/{{ $car->id }}">Supprimer</a>
        </td>
    </tr>
    @endforeach
</table>

</body>
</html>
3. resources/views/DetailCar.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Détails de la voiture</title>
</head>
<body>

<h2>Détails de la voiture</h2>

<p>Matricule : {{ $car->matricule }}</p>
<p>Marque : {{ $car->marque }}</p>
<p>Modèle : {{ $car->model }}</p>
<p>Type : {{ $car->type }}</p>

<a href="/lister-cars">Retour à la liste</a>

</body>
</html>
4. resources/views/ModifierCars.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Modifier la voiture</title>
</head>
<body>

<h2>Modifier la voiture</h2>

<form action="/mettre-a-jour-car/{{ $car->id }}" method="post">
    @csrf
    <label for="matricule">Matricule :</label>
    <input type="text" name="matricule" value="{{ $car->matricule }}" required><br>

    <label for="marque">Marque :</label>
    <input type="text" name="marque" value="{{ $car->marque }}" required><br>

    <label for="model">Modèle :</label>
    <input type="text" name="model" value="{{ $car->model }}" required><br>

    <label for="type">Type :</label>
    <input type="text" name="type" value="{{ $car->type }}" required><br>

    <button type="submit">Mettre à jour</button>
</form>

</body>
</html>

5. Routes

Ajoutez les routes dans le fichier routes/web.php :
use App\Http\Controllers\CarController;

Route::get('/ajouter-car', [CarController::class, 'ajouter'])->name("Route_ajouterCar");
Route::post('/sauvegarder-car', [CarController::class, 'sauvegarder'])->name("Route_sauvegarderCar");
Route::get('/lister-cars', [CarController::class, 'lister'])->name("Route_listeCars");
Route::get('/detail-car/{id}', [CarController::class, 'detail']);
Route::get('/modifier-car/{id}', [CarController::class, 'modifier']);
Route::post('/mettre-a-jour-car/{id}', [CarController::class, 'mettreAJour']);
Route::get('/supprimer-car/{id}', [CarController::class, 'supprimer']);;