1. Go to this page and download the library: Download illuminatech/enum-seeder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
illuminatech / enum-seeder example snippets
namespace Database\Seeders;
use Illuminatech\EnumSeeder\EnumSeeder;
class ItemCategorySeeder extends EnumSeeder
{
protected function table(): string
{
return 'item_categories';
}
protected function rows() : array
{
return [
[
'id' => 1,
'name' => 'Consumer goods',
'slug' => 'consumer-goods',
],
[
'id' => 2,
'name' => 'Health care',
'slug' => 'health-care',
],
// ...
];
}
}
// ...
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
// always synchronize all dictionary (enum) tables:
$this->call(ItemCategorySeeder::class);
$this->call(ItemStatusSeeder::class);
$this->call(ContentPageSeeder::class);
// ...
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemStatusTable extends Migration
{
public function up()
{
Schema::create('item_statuses', function (Blueprint $table) {
$table->unsignedSmallInteger('id')->primary(); // no sequence (autoincrement)
$table->string('name');
// ...
});
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateItemStatusTable extends Migration
{
public function up()
{
Schema::create('item_statuses', function (Blueprint $table) {
$table->string('id', 50)->primary();
$table->string('name');
// ...
});
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ContentPageSeeder extends EnumSeeder
{
protected function table(): string
{
return 'content_pages';
}
protected function rows() : array
{
return [
[
'id' => 1,
'name' => 'About Us',
'slug' => 'about-us',
'content' => '<div>...</div>',
],
[
'id' => 2,
'name' => 'How it works',
'slug' => 'how-it-works',
'content' => '<div>...</div>',
],
// ...
];
}
protected function shouldDeleteObsolete(): bool
{
return true; // always delete records from 'content_pages', which 'id' is missing at `rows()`
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ItemStatusSeeder extends EnumSeeder
{
protected function table(): string
{
return 'item_statuses';
}
protected function rows() : array
{
return [
// ...
];
}
protected function shouldDeleteObsolete(): bool
{
return $this->container->environment('local'); // allows deletion of the records only in "local" environment
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ItemStatusSeeder extends EnumSeeder
{
protected function table(): string
{
return 'item_statuses';
}
protected function rows() : array
{
return [
// ...
];
}
protected function shouldUpdateObsoleteWith(): array
{
// following attributes will be applied to the records, which 'id' is missing at `rows()`
return [
'deleted_at' => now(),
];
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ItemCategorySeeder extends EnumSeeder
{
protected function table(): string
{
return 'item_categories';
}
protected function rows() : array
{
return [
[
'id' => 1,
'name' => 'Active Category',
// no need to specify 'is_active' and 'created_at' all the time
],
[
'id' => 2,
'name' => 'Inactive Category',
'is_active' => false, // overrides the value from `shouldCreateWith()`
],
// ...
];
}
protected function shouldCreateWith(): array
{
// applies following attributes per each new created record:
return [
'is_active' => true,
'created_at' => now(),
];
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ItemCategorySeeder extends EnumSeeder
{
protected function table(): string
{
return 'item_categories';
}
protected function rows() : array
{
return [
// ...
];
}
protected function shouldUpdateExisting(): bool
{
return false; // disable existing records update
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ContentPageSeeder extends EnumSeeder
{
protected function table(): string
{
return 'content_pages';
}
protected function rows() : array
{
return [
[
'id' => 1,
'is_active' => true,
'slug' => 'about-us',
'title' => 'About Us', // default value, will be applied only on creation
'content' => '<div>...</div>', // default value, will be applied only on creation
],
// ...
];
}
protected function shouldUpdateExistingOnly(): array
{
// only 'is_active' and 'slug' will be synchronized, while 'title' and 'content' remains intact
return [
'is_active',
'slug',
];
}
}
use Illuminatech\EnumSeeder\EnumSeeder;
class ContentPageSeeder extends EnumSeeder
{
protected function table(): string
{
return 'content_pages';
}
protected function rows() : array
{
return [
// ...
];
}
protected function shouldUpdateExistingWith(): array
{
// attribute values to be applied per each row update.
return [
'updated_at' => now(),
];
}
}