2017-02-14 6 views
2

Ich habe versucht, die folgenden 3 Codes in einem Laravel 5.3-Projekt zu verwenden, um eine neue Dezimalspalte zu einer vorhandenen Tabelle hinzuzufügen. Aber es gibt jedes Mal denselben Fehler.Wie man eine Dezimalspalte zu einer vorhandenen Tabelle in Laravel hinzufügt 5.3 Migration

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost'); 
}); 

und

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost', ['default'=>0]); 
}); 

und

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost', ['default'=>'0,0']); 
}); 

Der Fehler war:

[Illuminate\Database\QueryException]                     
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use near ') not null' at line 1 (SQL: alter table `mileages` add `cost` decimal (,) not null) 

ich etwas fehle?

Antwort

2

Sie versuchen, verwenden, die nicht die richtige Syntax ist.

Erstellen Sie eine neue Migration, php artisan make:migrate add_cost_column_to_mileages.

Dann in Ihrer Migration, wollen Sie es so aussehen für:

Schema::table('mileages', function (Blueprint $table) { 
    $table->decimal('cost', 5,2); //Substitute 5,2 for your desired precision 
}); 

Und dies unten:

Schema::table('mileages', function (Blueprint $table) { 
    $table->dropColumn('cost'); 
}); 

Das von der Dokumentation ist here, obwohl sie es nicht tun deutlich machen.

+0

Ja, es funktioniert. Vielen Dank!. Auch '$ table-> addColumn ('dezimal', 'cost', ['default' => 0, 'total' => 8, 'places' => 2]);' funktioniert. – Eranda

Verwandte Themen