PHP code example of dweik / laravel-database-patching

1. Go to this page and download the library: Download dweik/laravel-database-patching 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/ */

    

dweik / laravel-database-patching example snippets


// database/patches/2024_08_04_104717_UpdateUserDefaults.php
use LaravelDatabasePatching\Interfaces\SQLPatchInterface;
use \Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

class UpdateUserDefaults implements SQLPatchInterface
{
    public function handler()
    {
        // Ensure the column exists before updating
        if (Schema::hasColumn('users', 'default_column')) {
            DB::table('users')->whereNull('default_column')->update(['default_column' => 'default_value']);
        }
    }
}

bash
php artisan sql-patch:make SomeClassName
bash
php artisan sql-patch
bash
php artisan sql-patch:make UpdateUserDefaults
bash
php artisan sql-patch