Laravel5.5: migration string to JSON - json

How to make a migration in Laravel 5.5 that change the datatype of a String to JSON? My column has image links and I want to change it to JSON to store more links.
I thought this was the best way?
change the string value of the column so it corresponds to JSON datatype
change the datatype of the column to JSON
I'm able to do the first step in mysql by:
UPDATE db.vendor_horses SET image= CONCAT('{"images":["', image, '"]}');
Thanks!

You can easily create a new migration with php artisan make:migration migration_name command.
Use the following to change the datatype
Schema::table('vendor_horses', function (Blueprint $table) {
$table->json('image')->change();
});
The change() method allows you to set nullable, modify data length and types. Read more about change()

this just supported mySql version 8 to up
Schema::table('table_name', function (Blueprint $table) {
$table->json('field_name')->change();
});

First, If you use laravel and change your database table field then you install composer require doctrine/dbal Read more about modifying a column in laravel
Simply create a new migration file from php artisan make:migration migration_name and then open newly created migration file
Schema::table('your_table_name',function(Blueprint $table){
$table->json('image')->change();
//make nullable to image field
$table->json('image')->nullable()->change();
//rename image field to other
$table->renameColumn('old_column_name', 'new_column_name');
})
The change() method allow you to set data length , change your column name, and make nullable

Related

Laravel: Unknown column type "timestamp" requested

I am trying to perform a migration and I am getting the following problem.
Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
My code is the following:
Schema::table('XXXXXXXX', function (Blueprint $table) {
$table->timestamp('start')->change();
$table->timestamp('end')->change();
});
The strange thing is that I have already performed migrations with that type of data:
Schema::create('XXXXXX', function (Blueprint $table) {
...
$table->timestamp('date_expired')->nullable();
...
});
Does anyone know how to fix it or see the error I'm doing.
Thanks
UPDATE
In the end I have deleted the migration, I have modified it putting timestamp in the necessary columns and I have executed it again. (Having deleted the table before from the database)
On the laravel docs page you can find a warning telling you that there are certain types that you can't use with the ->change() method.
Link to laravel docs
It also says this:
To modify a timestamp column type a Doctrine type must be registered.

How to Alter Blob to Varchar in Laravel Make:Migration

I need to change a Blob field type to a Varchar(128). The data in the field will fit the target field size, it's text, and shouldn't have a problem with UTF-8.
Sample data, all data is in this format:
{"weight":"0","height":"0","width":"0","length":"0"}
I'm using Laravel Make:migrate to handle the conversion.
How should I write the SQL?
I know how to write a Laravel Migration. I also know how to alter a field. But a Blob isn't a text field nor is a Blob normally converted down to a Varchar. I've done some manual UTF-8 conversion of Blobs in the past and know you can mess up your data if you don't do it right. So my concern here is to not mess up my data with a Laravel Migrate. I don't believe the migrate 'down' method can undo corrupted data.
If my data fits the varchar target size and if the data fits the UTF-8 charset, am I good with a straight forward Alter statement:
DB::query("ALTER TABLE DB1.products CHANGE COLUMN dimensions dimensions varchar(128) DEFAULT NULL;");
You shouldn't use sql for this, just create a migration and use change method
Schema::table('table_name', function ($table) {
$table->string('column_name')->change();
});
https://laravel.com/docs/5.7/migrations#modifying-columns
Considering your comment sql would be
ALTER TABLE tablename MODIFY column_name VARCHAR(128);
Run composer install and then composer update in the console and
drop your table from the database and also delete the migration ,then create a new migration using
php artisan make:migration change_col_datatype --table=table_name
and then make changes as below
Schema::table('your_table_name', function ($table) {
$table->string('your_table_name');
});
public function down()
{
Schema::dropIfExists('tablename');
}
The SQL statment:
\DB::statement('ALTER TABLE products CHANGE COLUMN dimensions dimensions VARCHAR(128) DEFAULT NULL;');
Worked fine.

Are there any way to create singular-name DB table?

development environment
Lnaguage : Golang ver.1.9.2
DB : mySQL
Framework : not decided (Maybe I'll use revel)
situation
I already have DB which has singular-name table ,like "user", "page". It can't be changed.
Now I'll develop new application using this DB.
I created simple application to connect this DB, and tried to auto migrate using gorm(https://github.com/jinzhu/gorm).
I defined some models, like "user" which is same as existing DB table name, and run auto-migrate just as it written in (http://jinzhu.me/gorm/database.html#connecting-to-a-database )
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
Then, new table "users" was created.
Question
Can I create singular-name table, like "user" with auto-migrate or those things ?
Using gorm is not required, so I'll use another orm library if it works.
I hope anyone help me !
Implement function TableName on struct User to return a custom name for the table. The ORM uses this function to get the table name for all DB operations.
func (user *User) TableName() string {
return "user"
}
Refer docs here: http://jinzhu.me/gorm/models.html#table-name-is-the-pluralized-version-of-struct-name
You have set with db instance to use singular table, like this:
db.SingularTable(true)

database error after adding new column

I had created a login and register form. But I forgot to add a column. So, I wanted to add a new column named "contactNumber". For adding the column, I wrote a command named php artisan migrate:refresh in cmd.exe and also write a code at create_users_table in migration folder. But, the new column's value doesn't go to mysql database. What am I doing wrong?
You need to add your new column to your model, so in your User class you need to do this:
protected $fillable = [
'name', 'email', 'password', 'contactNumber'
];
Hope this help
You're trying to insert a new record in the database but the contactNumber column doesn't have a default value. In the migration file you can add nullable to that column, eg.
$table->string('contactNumber')->nullable();
or directly in the database from a GUI.
For this type of scenario i personally create new migration like this
php artisan migrate:make add_column_to_table --table="tableName"
Now to run the new migration run this command.
php artisan migrate
This will add new column in your table.Also have a look into this thread.
https://laracasts.com/discuss/channels/laravel/add-new-column-to-existing-table

Yii2 :: How to change existing enum list?

I have in my database enum list ('name_1','name_2','name_3'). Also I have create Model and Controller.
Later I have change enum list to ('new_name_1','new_name_2','new_name_3').
But in form I still see old list ('name_1','name_2','name_3').
What must I do to refresh enum list?
Thank you.
You should use Alter Table Modify
ALTER TABLE your_db.your_table
MODIFY Your_enum_col ENUM('new_name_1','new_name_2','new_name_3');
Rather simple. As #scaisEdge commented, you need to execute an update query.
On a migration you can execute the following code to edit an ENUM column on Yii2.
public function safeUp()
{
$query = "ALTER TABLE TABLENAME MODIFY tipo ENUM('VALUE1','VALUE2','VALUE4','VALUE5') default 'VALUE1'";
$this->execute($query);
}
In this case we are also giving the default value of VALUE1, in case that we don't provide a value.
Remember that you can create the migration from the console using the yii command.
./yii migrate/create add_enum_value_to_table
The migration should be on the console folder, under migrations.
Hope that it helps !