You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9迁移中如何使用raster数据类型?

解决Laravel 9迁移中raster类型方法不存在的问题

错误原因:Laravel原生的Schema\Blueprint并未内置raster字段类型的支持,该类型是PostGIS专属的空间数据类型,不属于Laravel默认支持的字段范畴。

解决方案

方案一:直接使用原生数据库类型定义

无需额外安装包,通过Laravel的原生方法或SQL语句来添加raster列:

方法1:使用addColumn指定自定义类型

修改迁移代码中的raster字段定义:

Schema::create('bi.defaultplant_date', function (Blueprint $table) {
    $table->bigIncrements('rid');
    $table->addColumn('raster', 'rast'); // 替换原$table->raster('rast');
    $table->text('filename');
});

方法2:执行原生SQL语句添加列

如果上述方法不生效,可先创建基础表,再通过原生语句添加raster列:

public function up()
{
    Schema::create('bi.defaultplant_date', function (Blueprint $table) {
        $table->bigIncrements('rid');
        $table->text('filename');
    });

    // 添加raster列
    DB::statement('ALTER TABLE bi.defaultplant_date ADD COLUMN rast raster;');
}

方案二:使用PostGIS扩展包

安装适配Laravel 9的laravel-postgis包,获得原生的raster()方法支持:

  1. 安装扩展包:
composer require grimzy/laravel-postgis
  1. 修改迁移文件,引入PostGIS的Blueprint类:
use Grimzy\LaravelPostgis\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

// 注册PostGIS的Blueprint
Builder::blueprintResolver(function($table, $callback) {
    return new Blueprint($table, $callback);
});

Schema::create('bi.defaultplant_date', function (Blueprint $table) {
    $table->bigIncrements('rid');
    $table->raster('rast');
    $table->text('filename');
});

前置要求

无论使用哪种方案,你的数据库必须已启用PostGIS及PostGIS Raster扩展,可在迁移的up方法中添加以下语句确保扩展存在:

DB::statement('CREATE EXTENSION IF NOT EXISTS postgis;');
DB::statement('CREATE EXTENSION IF NOT EXISTS postgis_raster;');

内容的提问来源于stack exchange,提问作者samzna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 11:57:23