Skip to content

Commit

Permalink
seeders for test data
Browse files Browse the repository at this point in the history
  • Loading branch information
Thombrix committed Oct 29, 2023
1 parent 66dabe9 commit 5de96bb
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function up(): void
{
Schema::create('langues_villes', function (Blueprint $table) {

$table->unsignedBigInteger('author_id');
$table->unsignedBigInteger('book_id');
$table->primary(['author_id', 'book_id']);
$table->unsignedBigInteger('langue_id');
$table->unsignedBigInteger('ville_id');

$table->primary(['langue_id', 'ville_id']);

$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function up(): void
{
Schema::table('langues_villes', function (Blueprint $table) {

$table->foreignId('langue_id')->constrained()->cascadeOnDelete();
$table->foreignId('ville_id')->constrained()->cascadeOnDelete();
$table->foreign('langue_id')->references('id')->on('langues')->cascadeOnDelete();
$table->foreign('ville_id')->references('id')->on('villes')->cascadeOnDelete();
});
}

Expand Down
53 changes: 53 additions & 0 deletions backend-laravel/database/seeders/CantonSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class CantonSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$cantons =
[
['name' => 'Jura', 'img' => 'jura.png'],
['name' => 'Zurich', 'img' => 'zurich.png'],
['name' => 'Bern', 'img' => 'bern.png'],
['name' => 'Lucerne', 'img' => 'lucerne.png'],
['name' => 'Uri', 'img' => 'uri.png'],
['name' => 'Schwyz', 'img' => 'schwyz.png'],
['name' => 'Obwalden', 'img' => 'obwalden.png'],
['name' => 'Nidwalden', 'img' => 'nidwalden.png'],
['name' => 'Glarus', 'img' => 'glarus.png'],
['name' => 'Zug', 'img' => 'zug.png'],
['name' => 'Fribourg', 'img' => 'fribourg.png'],
['name' => 'Solothurn', 'img' => 'solothurn.png'],
['name' => 'Basel-Landschaft', 'img' => 'basel-landschaft.png'],
['name' => 'Basel-Stadt', 'img' => 'basel-stadt.png'],
['name' => 'Schaffhausen', 'img' => 'schaffhausen.png'],
['name' => 'Appenzell Ausserrhoden', 'img' => 'appenzell-ausserrhoden.png'],
['name' => 'Appenzell Innerrhoden', 'img' => 'appenzell-innerrhoden.png'],
['name' => 'St. Gallen', 'img' => 'st-gallen.png'],
['name' => 'Grisons', 'img' => 'grisons.png'],
['name' => 'Aargau', 'img' => 'aargau.png'],
['name' => 'Thurgau', 'img' => 'thurgau.png'],
['name' => 'Ticino', 'img' => 'ticino.png'],
['name' => 'Vaud', 'img' => 'vaud.png'],
['name' => 'Valais', 'img' => 'valais.png'],
['name' => 'Neuchâtel', 'img' => 'neuchatel.png'],
['name' => 'Geneva', 'img' => 'geneva.png']
];

foreach ($cantons as $canton)
{
\App\Models\Canton::create(array(
'name' => $canton["name"],
'img' => $canton["img"]
));
}
}
}
5 changes: 5 additions & 0 deletions backend-laravel/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ public function run(): void
// 'name' => 'Test User',
// 'email' => '[email protected]',
// ]);

$this->call(LangueSeeder::class);
$this->call(CantonSeeder::class);
$this->call(VilleSeeder::class);
$this->call(LangueVilleSeeder::class);
}
}
30 changes: 30 additions & 0 deletions backend-laravel/database/seeders/LangueSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class LangueSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$langues =
[
['name' => 'Français'],
['name' => 'Suisse-Allemand'],
['name' => 'Italien'],
['name' => 'Romanche']
];

foreach ($langues as $langue)
{
\App\Models\Langue::create(array(
'name' => $langue["name"]
));
}
}
}
33 changes: 33 additions & 0 deletions backend-laravel/database/seeders/LangueVilleSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class LangueVilleSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$langues_villes = [
['ville_id' => 1, 'langue_id' => 1],
['ville_id' => 2, 'langue_id' => 2],
['ville_id' => 3, 'langue_id' => 1],
['ville_id' => 3, 'langue_id' => 2],
['ville_id' => 4, 'langue_id' => 2]

];

foreach ($langues_villes as $langue_ville)
{
DB::table('langues_villes')->insert([
'ville_id' => $langue_ville['ville_id'],
'langue_id' => $langue_ville['langue_id']
]);
}
}
}
35 changes: 35 additions & 0 deletions backend-laravel/database/seeders/VilleSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class VilleSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$cities = [
['name' => 'Lausanne', 'img' => 'lausanne.png', 'ecusson' => 'lausanne-ecu.png', 'population' => 140000, 'coord' => '46.519961, 6.633597', 'canton_id' => 23],
['name' => 'Zurich', 'img' => 'zurich.png', 'ecusson' => 'zurich-ecu.png', 'population' => 400000, 'coord' => '47.376886, 8.541694', 'canton_id' => 2],
['name' => 'Bern', 'img' => 'bern.png', 'ecusson' => 'bern-ecu.png', 'population' => 130000, 'coord' => '46.948020, 7.447433', 'canton_id' => 3],
['name' => 'Lucerne', 'img' => 'lucerne.png', 'ecusson' => 'lucerne-ecu.png', 'population' => 82000, 'coord' => '47.050168, 8.309307', 'canton_id' => 4]
];

foreach ($cities as $city)
{
\App\Models\Ville::create(array(
'name' => $city["name"],
'img' => $city["img"],
'ecusson' => $city["ecusson"],
'population' => $city["population"],
'coord' => $city["coord"],
'canton_id' => $city["canton_id"]
));
}

}
}

0 comments on commit 5de96bb

Please sign in to comment.