Created
November 22, 2022 13:21
-
-
Save j-dexx/c0645a32659024a2929d0849fa580de4 to your computer and use it in GitHub Desktop.
Using mysql native types that are not supported in laravel's schema builder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Doctrine\DBAL\Platforms\MySqlPlatform; | |
use Doctrine\DBAL\Types\BlobType; | |
use Doctrine\DBAL\Types\Type; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Schema\Grammars\Grammar; | |
use Illuminate\Support\ServiceProvider; | |
class DatabaseMacroProvider extends ServiceProvider | |
{ | |
/** | |
* Register services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Type::addType('mediumblob', BlobType::class); | |
Grammar::macro('typeMediumBlob', function () { | |
return 'mediumblob'; | |
}); | |
Blueprint::macro('mediumBlob', function (string $column) { | |
return $this->addColumn('mediumBlob', $column, [ | |
'length' => MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB, | |
]); | |
}); | |
Blueprint::macro('blob', function (string $column) { | |
// Otherwise doctrine uses the length of the column by default and change to a binary column | |
// does nothing. | |
return $this->addColumn('binary', $column, [ | |
'length' => MySqlPlatform::LENGTH_LIMIT_BLOB, | |
]); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Schema; | |
class MyMigration extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::table('some_table', function (Blueprint $table) { | |
$table->mediumBlob('data')->change(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::table('some_table', function (Blueprint $table) { | |
$table->blob('data')->change(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment