Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
Terminal
composer create-project laravel/laravel example-app
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_first_step
DB_USERNAME=root
DB_PASSWORD=
Terminal
php artisan make:model Pays -mc
database/migrations/2022_03_11_112912_create_regions_table.php
public function up()
{
$table->id();
$table->string("name")->unique();
$table->timestamps();
}
Terminal
php artisan migrate
Terminal
php artisan make:model Regions -mc
database/migrations/.....create_regions_table.php
public function up()
{
$table->id();
$table->string("name")->unique();
$table->timestamps();
}
NB: Pour Annuler les migrations
Cela supprime les derniers migrations qui ont ete faitesphp artisan migrate:rollack
php artisan migrate
database/migrations/.....create_regions_table.php
public function up()
{
//....
$table->foreignId('pays_id')->constrained('pays')
->onUpdate('cascade')
->onDelete('cascade');
}
Terminal
php artisan migrate:fresh
Terminal
php artisan make:factory PaysFactory
PaysFactory.php
public function definition()
{
return [
'nom'=> $this->faker->company(),
'indicatif' => $this->faker->areaCode
];
}
Terminal
php artisan make:seeder PaysSeeder
PaysSeeder.php
public function run()
{
Pays::factory()->count(3)->create();
}
php artisan db:seed --class=PaysSeeder
Terminal
php artisan make:factory RegionFactory
RegionFactory
public function definition()
{
return [
"nom"=> $this->faker->unique()->country(),
"pays_id"=> $this->faker->numberBetween(1, 10)
];
}
Terminal
php artisan make:seeder RegionSeeder
Configurons notre seeder pour qu'il nous genere 30 regions et configuer le à ce qu'il soit dans un pays exisants dans notre base de donnee
RegionSeeder.php
public function run()
{
return Region::factory()
->count(30)
->state(new Sequence(
fn($sequence) => ['pays_id' => Pays::all()->random()],
))
->create();
}
Terminal
php artisan db:seed --class=RegionSeeder
routes/web.php
Route::get('/pays', [PaysController:: class, 'index']);
PaysController.php
public function index()
{
return 'Hello World';
}
- id du pays
- indicatif du pays
- nombre de regions
select count(r.id) as nombre , pays_id as id, p.nom as pays from regions as r join pays p on p.id = r.pays_id group by pays_id;
PaysController.php
public function index()
{
$paysRegions = DB::table('regions')
->select(DB::raw('pays_id as id, pays.nom as pays, indicatif , count(regions.id) as nombre_regions'))
->groupBy('pays_id', 'pays.nom', 'pays.indicatif')
->join('pays', 'pays.id', '=', 'pays_id')
->get();
return view('pays.index', [
'paysRegions' => $paysRegions
]);
}
2-c)Configurons notre vue à qu'il recupere le tableau renvoyé par notre vue et l'affiche sur une balise table
ressouces/views/pays/index.blade.php
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Indicatif</th>
<th scope="col">Nom</th>
<th scope="col">Nbre regions</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($paysRegions as $pays)
<tr>
<th scope="row">{{$loop->index}}</th>
<td>{{$pays->indicatif}}</td>
<td>{{$pays->pays}}</td>
<td>{{$pays->nombre_regions}}</td>
<td>
<button type="button" class="btn btn-sm btn-primary">Regions</button>
</td>
</tr>
@endforeach
</tbody>
</table>
web.php
Route::get('/pays/{id}', [PaysController:: class, 'getRegions'])->whereNumber('id');
PaysController.php
public function getRegions($id)
{
$pays= Pays::find($id);
$regions = Region::all()->where('pays_id', "=", $id);
return view('pays.regions', [
'regions' => $regions,
'pays' => $pays
]);
}
2-e) Configurons notre vue à ce qu'il recupere le tableau envoyé par la fonction et les affiche dans une balise table
ressouces/views/pays/regions.blade.php
<div class="container my-5 pt-5">
<div class="container">
<h1><span class="fw-bold">Pays: </span> {{$pays->nom}}</h1>
</div>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nom</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($regions as $region)
<tr>
<th scope="row">{{$loop->index}}</th>
<td>{{$region->nom}}</td>
<td>
<button type="button" class="btn btn-sm btn-primary"><i
class="fa-solid fa-arrow-up-right-from-square"></i></button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
Generons notre model Departement, Commune, Quartier et Quartier,Entreprise avec migration et controller
Terminal
php artisan make:model Departement -mc
php artisan make:model Commune -mc
php artisan make:model Quartier -mc
php artisan make:model Entreprise -mc
Terminal
php artisan make:factory DepartementFactory
DepartementFactory
public function definition()
{
return [
"nom"=> $this->faker->unique()->country(),
"pays_id"=> $this->faker->number()
];
}
Terminal
php artisan make:seeder DepartementSeeder
Configurons notre seeder pour qu'il nous genere 60 regions et configuer le à ce qu'il soit dans un pays exisants dans notre base de donnee
DepartementSeeder.php
public function run()
{
return Departement::factory()
->count(60)
->state(new Sequence(
fn($sequence) => ['region_id' => Region::all()->random()],
))
->create();
}
Terminal
php artisan db:seed --class=DepartementSeeder