Search code examples
laravelschemalaravel-8

How to set indexes when adding columns to a table in Laravel?


The column I added appears after created_at and other columns. I use a custom method to create columns via CRM, which is why I add columns manually like this.

public function addColumnTable($table_name,$field_name,$field_type){
        Schema::table($table_name, function (Blueprint $table) use($field_name,$field_type) {
            $table->{$field_type}($field_name);
        });
    }

Solution

  • I had to use it:

    public function addColumnTable($table_name,$field_name,$field_type){
        Schema::table($table_name, function (Blueprint $table) use($field_name,$field_type) {
            $table->{$field_type}($field_name)->after('id');
        });
    }