How to create database, table, and migrate without artisan command
Comment (0)
Admin
138
1. Create the Database
You need to create your database manually or programmatically if it’s not already created.
Manually:
- Log in to your database management system (e.g., MySQL, PostgreSQL).
 - Execute SQL commands to create a new database.
 
For example, in MySQL, you might use:
# sql
CREATE DATABASE your_database_name; 
Programmatically (MySQL Example):
- Use a database connection script to run the creation command.
 
# php
use Illuminate\Support\Facades\DB;
DB::statement('CREATE DATABASE your_database_name'); 
Ensure your .env file has the correct database connection settings.
2. Create Migrations Manually
Instead of running the php artisan make:migration command, you’ll manually create migration files. Migration files are typically stored in database/migrations and have a timestamp as part of the filename.
Example of a Migration File: Create a new PHP file in database/migrations (e.g., 2024_08_20_000000_create_users_table.php).
# php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration { 
    public function up() { 
        Schema::create('users', function (Blueprint $table) {
            $table->id(); $table->string('name');
            $table->string('email')->unique(); 
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken(); 
            $table->timestamps();
    }); 
}
    public function down() { 
        Schema::dropIfExists('users'); 
    } 
} 
3. Run Migrations Programmatically
You can execute migrations programmatically using the Laravel Migration classes. For this, you’ll use Laravel’s Migration class and Artisan commands directly within your PHP code.
Example Script to Run Migrations:
# php
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Facades\Artisan;
// Make sure to include necessary bootstrap code if running outside of Laravel context
// Call Artisan migrate command 
Artisan::call('migrate'); 
If you’re not using Artisan and want to manually invoke migrations:
# php
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Facades\App;
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$migrator = App::make(Migrator::class);
$migrator->run(database_path('migrations')); 
4. Handle Rollbacks
To roll back migrations programmatically, you can use:
# php
Artisan::call('migrate:rollback'); 
Or manually, if bypassing Artisan:
# php
$migrator->rollback(database_path('migrations')); 
Notes
- 
	
Seeding: If you need to seed your database, you’ll also need to handle this manually. Use
Artisan::call('db:seed')or invoke seeders directly. - 
	
Testing: Ensure you test these procedures in a development environment before running them in production.
 - 
	
Environment Configuration: Ensure that your
.envfile and database configuration are correctly set up. 
                    
                
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                            
Comments (0)
Your Comment