PHP code example of robinncode / laravel-db-craft

1. Go to this page and download the library: Download robinncode/laravel-db-craft 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/ */

    

robinncode / laravel-db-craft example snippets


return [
    // Database connection to use
    'connection' => env('DB_CRAFT_CONNECTION', null),
    
    // Path where migration files will be stored
    'migrations_path' => database_path('migrations'),
    
    // Path where seeder files will be stored
    'seeders_path' => database_path('seeders'),
    
    // Tables to exclude from generation
    'exclude_tables' => [
        'migrations',
        'password_resets',
        'password_reset_tokens',
        'failed_jobs',
        'personal_access_tokens',
    ],
    
    // Number of records per chunk in seeders
    'seeder_chunk_size' => 100,
];



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id('id');
            $table->string('name');
            $table->string('email');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            
            $table->unique('email');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};



namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
    public function run(): void
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        
        DB::table('users')->truncate();
        
        DB::table('users')->insert([
            [
                'id' => 1,
                'name' => 'John Doe',
                'email' => '[email protected]',
                'created_at' => '2024-01-01 00:00:00',
                'updated_at' => '2024-01-01 00:00:00',
            ],
            // More records...
        ]);
        
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
}
bash
php artisan vendor:publish --provider="RobinNcode\LaravelDbCraft\DbCraftServiceProvider"
bash
php artisan get:migration
bash
php artisan get:migration users
bash
php artisan get:migration --connection=mysql
bash
php artisan get:seeder
bash
php artisan get:seeder users
bash
php artisan get:seeder --connection=mysql
bash
# Generate seeders for all tables
php artisan get:seeder

# Or for specific critical tables
php artisan get:seeder users
php artisan get:seeder products
php artisan get:seeder orders