Skip to content

Instantly share code, notes, and snippets.

@j-dexx
Created November 22, 2022 13:21
Show Gist options
  • Save j-dexx/c0645a32659024a2929d0849fa580de4 to your computer and use it in GitHub Desktop.
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
<?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,
]);
});
}
}
<?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