This is my migration code. This is not working not changing column collation.
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collate('utf8mb4_bin')->change();
});
}
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collation('utf8mb4_bin')->change();
});
}
Related
The column I added appears after created_at and other columns.
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);
});
}
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');
});
}
In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}
So, I want to add a column ('Semester_TA') and change the property of this column ('kode_mk') to be unique value, so I created this migration:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
Schema::table('lr2', function(Blueprint $table){
$table->dropForeign(['id']);
});
}
public function down()
{
//
}
I also want to drop/delete this foreign key ('id') that I created as it sometimes give a constraint error in my project. My question is what function down should I write to reverse the update above that i'm about to do in case of migration fails or error?
this is the table "mk" and "lr2" migrations that i use right now:
Schema::create('mk', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('kode_mk');
$table->string('mk');
$table->integer('sks');
$table->integer('semester');
$table->integer('available_seats');
$table->timestamps();
});
Schema::create('lr2', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k_mk');
$table->string('mk');
$table->string('angkatan');
$table->integer('request_seats');
$table->boolean('status_request');
$table->timestamps();
$table->foreign('id')->references('id')->on('mk');
});
First, you have to create a new migration:
php artisan make:migration Add_column_change_the_property
Then in that migration file up(), try:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
}
Then in the migration file down() try:
public function down()
{
$table->dropColumn('kode_mk');
$table->foreign('id')->references('id')->on('mk');
//
}
Then migrate the file by:php artisan migrate
I want to add a foreign key to my schools table but the primary key of the provinces table is not added to the foreign key reference.
This is my Schools Table
public function up()
{
Schema::create('schools', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name");
$table->unsignedTinyInteger("province_id");
});
}
and Provinces Table
public function up()
{
Schema::create('provinces', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",255);
});
}
and this is my Foreign key constraint
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->foreign('province_id')->reference('id')->on('provinces');
});
}
and this is the error
enter image description here
If there is a solution tell me.
Thanks.
Try this and also check the sequence of your migrations.
public function up()
{
Schema::create('student_profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",64);
$table->string("father_name",64);
$table->string("last_name",64);
$table->string("grand_father_name",64);
$table->string("graduation_year",4)->unique();
$table->enum("gender",['0','1']);
$table->unsignedSmallInteger('school_id')->nullable();
$table->unsignedTinyInteger("province_id")->nullable();
$table->foreign('school_id')->reference('id')->on('schools')-
>onDelete('set null');
$table->foreign('province_id')->reference('id')->on('provinces')->onDelete('set null');
});
}
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->unsignedBigInteger('province_id')->nullable();
$table->foreign('province_id')->reference('id')->on('provinces')
->onDelete('set null');
});
}
This is my current code.
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(1);
});
}`
How to change the default value to 0 as below?
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(0);
});
}
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(0)->change();
});
}
Added ->change(). Please see Link
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_active')->unsigned()->nullable();
$table->timestamps();
});
is_active column is defined as a nullable boolean column using the boolean data type and the unsigned modifier. This will allow you to use the true and false keywords in your database queries instead of 0 and 1.