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





client(idclient ,nom,prenom)
categorie(idcategorie,nom,description)
produit(idproduit,#idcategorie,nom ,prix,quantiteStock,marque)
vente(idvente,#idclient,#idproduit,quantite,datevente)
1.Migration

To create migrations for the four tables (client, categorie, produit, vente) in Laravel, you can follow these steps:

Step 1: Open the terminal and navigate to your Laravel project directory.

Step 2: Run the following command to create a migration for the client table:

go
php artisan make:migration create_clients_table --create=clients

This command will generate a migration file in the database/migrations directory with a name that includes the current timestamp and the name of the table being created.

Step 3: Open the migration file (located in database/migrations) for the client table and add the following code to the up() method:

lua
Schema::create('clients', function (Blueprint $table) {
$table->id('idclient'); $table->string('nom');
$table->string('prenom'); $table->timestamps();
});

This will create the clients table with the specified columns.

Step 4: Run the following command to create a migration for the categorie table:

go
php artisan make:migration create_categories_table --create=categories

Step 5: Open the migration file for the categorie table and add the following code to the up() method:

lua
Schema::create('categories', function (Blueprint $table) {
$table->id('idcategorie');
$table->string('nom');
$table->text('description');
$table->timestamps();
});

This will create the categories table with the specified columns.

Step 6: Run the following command to create a migration for the produit table:

go
php artisan make:migration create_produits_table --create=produits

Step 7: Open the migration file for the produit table and add the following code to the up() method:

lua
Schema::create('produits', function (Blueprint $table) {
$table->id('idproduit');
$table->foreignId('idcategorie')->constrained('categories');
$table->string('nom');
$table->decimal('prix', 10, 2);
$table->integer('quantiteStock');
$table->string('marque');
$table->timestamps();
});

This will create the produits table with the specified columns, including a foreign key constraint to link each product to its category.

Step 8: Run the following command to create a migration for the vente table:

go
php artisan make:migration create_ventes_table --create=ventes

Step 9: Open the migration file for the vente table and add the following code to the up() method:

bash
Schema::create('ventes', function (Blueprint $table) {
$table->id('idvente');
$table->foreignId('idclient')->constrained('clients');
$table->foreignId('idproduit')->constrained('produits');
$table->integer('quantite'); $table->date('datevente');
$table->timestamps(); });

2.Models

Here are the model classes:

php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Client extends Model {
protected $table = 'clients';
protected $primaryKey = 'idclient';
protected $fillable = ['nom', 'prenom'];
public function ventes() {
return $this->hasMany(Vente::class, 'idclient');
}
}
php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categorie extends Model {
protected $table = 'categories';
protected $primaryKey = 'idcategorie';
protected $fillable = ['nom', 'description'];
public function produits() {
return $this->hasMany(Produit::class, 'idcategorie');
}
}
php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Produit extends Model {
protected $table = 'produits';
protected $primaryKey = 'idproduit';
protected $fillable = ['idcategorie', 'nom', 'prix', 'quantiteStock', 'marque'];
public function categorie() {
return $this->belongsTo(Categorie::class, 'idcategorie');
}
public function ventes() {
return $this->hasMany(Vente::class, 'idproduit');
}
}
php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Vente extends Model {
protected $table = 'ventes';
protected $primaryKey = 'idvente';
protected $fillable = ['idclient', 'idproduit', 'quantite', 'datevente'];
public function client() {
return $this->belongsTo(Client::class, 'idclient');
}
public function produit({
return $this->belongsTo(produit::class, 'idproduit');
}
}

ClientCntroller.php

  1. Next, create the controller using the following command in the terminal:
go
php artisan make:controller ClientController --resource --model=Client
  1. This will generate a "ClientController.php" file in the "app/Http/Controllers" directory. Open this file and you will see a skeleton controller with empty methods for each of the CRUD operations.

  2. Fill in each of the methods as follows:

  • index: This method should return a view that lists all of the clients in the database.
php
public function index() {
$clients = Client::all();
return view('clients.index', compact('clients'));
}
  • create: This method should return a view with a form for creating a new client.
php
public function create() {
return view('clients.create');
}
  • store: This method should store the new client in the database and redirect back to the index page.
php
public function store(Request $request) {$client = new Client;
Il faut ajouter la valition
$client->nom = $request->nom;
$client->prenom = $request->prenom; $client->save();
return redirect()->route('clients.index');
}
  • show: This method should return a view with the details of a specific client.
php
public function show(Client $client) {
return view('clients.show', compact('client'));
}
  • edit: This method should return a view with a form for editing a specific client.
php
public function edit(Client $client) {
return view('clients.edit', compact('client'));
}
  • update: This method should update the specific client in the database and redirect back to the index page.
php
public function update(Request $request, Client $client) {
$client->nom = $request->nom; $client->prenom = $request->prenom; $client->save();
return redirect()->route('clients.index');
}
  • destroy: This method should delete the specific client from the database and redirect back to the index page.
php
public function destroy(Client $client) {
$client->delete();
return redirect()->route('clients.index');
}
  1. Finally, define the routes for the controller in the "routes/web.php" file:
php
Route::resource('clients', 'ClientController');