How does it work if I want to add a new column in my production table? I know that I will loose all the data when I make a migration:fresh.
1) is it possible to change the migration and add a new column as I did with the fastnetnr column?
public function up()
{
Schema::create('kontaktforms', function (Blueprint $table) {
$table->increments('id');
$table->string('navn');
$table->string('mobilnr');
//new fastnetnr column added
$table->string('fastnetnr')->nullable();
$table->string('mail');
$table->string('emne');
$table->text('beskrivelse');
$table->timestamps();
});
2) Or do I have to add a new column with php artisan so the output is like this?
public function up()
{
Schema::table('kontaktforms', function($table) {
$table->string('fastnetnr')->nullable();
});
}
Logically you can do it by following steps;
rollback your table php artisan migrate:rollback or php artisan
migrate:rollback --step=1
edit your migration file
migration back php artisan migrate
but it's not a best practice, especially in production. so you have to create new migration file for modify table and migrate it.
create new migration file
php artisan make:migration add_fastnetnr_to_kontaktforms_table --table=kontaktforms
and add new column like this
public function up()
{
Schema::table('kontaktforms', function (Blueprint $table) {
$table->string('fastnetnr')->nullable();
});
}
public function down()
{
Schema::table('kontaktforms', function (Blueprint $table) {
$table->dropColumn('fastnetnr');
});
}
and migrate
php artisan migrate
Related
I have this migration:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
And now I want to add a new column, and looks like this:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('pathlogo')->default('/');
$table->timestamps();
});
}
How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.
Now I have some data in DB which not exist on seed I just want to make a new column.
You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:
$table->string('pathlogo')->default('/')->after('slug');
after will ensure the column is positioned how you want.
If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.
Create a new migration and Use that as your up function:
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('pathlogo')->default('/');
});
}
them migrate as normal.
My migration is like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id_test');
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
I want to change data type and add column in table test. Then update table in database. I edit like this :
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->string('id_test', 18);
$table->string('name', 50);
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('tests');
}
Then I run : php artisan migrate:refresh
I lost my data in database
Is there any people who can help me?
Create a new migration file using below command :
php artisan make:migration name_of_your_file
And its function should be like this
public function up()
{
Schema::table('tests', function($table) {
$table->renameColumn('id_test', 'id');
$table->string('id_test', 18);
});
}
public function down()
{
Schema::table('tests', function($table) {
$table->dropColumn('id_test');
});
}
Now just run below command
php artisan migrate
Note : Dont use refresh it deletes the data
To know more about Laravel Migration refer : https://laravel.com/docs/5.3/migrations#modifying-columns
Is there a way to set auto increment back to 1 before seeding a table?
I empty the table before seeding it and if I didn't do migrate:refresh before seeding it then it continues the auto increment of the ID from the last position, e.g., 4.
Table seed:
public function run()
{
DB::table('products')->delete();
// Product table seeder
$product = new \App\Product([
'category_id' => 1,
'image_path' => '/images/products/1/000001.jpg',
'title' => 'test',
]);
$product->save();
}
Creating the table:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string('image_path');
$table->string('title');
$table->timestamps();
});
Try this:
DB::statement('SET FOREIGN_KEY_CHECKS=0');
DB::table('products')->truncate();
Instead of
DB::table('products')->delete();
If you're using make:migration or make:model -m commands to create a migration, Laravel is creating down() method with dropIfExists() clause:
public function down()
{
Schema::dropIfExists('products');
}
So when you run migrate:refresh command, Laravel will drop the table and will recraete it for you.
Also, you have foreign keys in the table, so you need to use dropForeign() first:
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropForeign('products_category_id_foreign');
});
Schema::dropIfExists('products');
}
I created table migrations in Laravel using php artisan migrate:make. When I tried to create the tables in the database, I got an error:
[ErrorException]
Creating default object from empty value
What is this related to? No tables are created nor can I find any errors in my migrations.
I have 25 tables in the migrations folder, all look similar to this.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAddressesTable extends Migration {
public function up() {
Schema::create("addresses", function() {
$table->engine = "InnoDB";
$table->increments("id");
$table->integer("user_id")->unsigned();
$table->string("street");
$table->string("city");
$table->integer("postal_code")->unsigned();
$table->foreign("user_id")->references("id")->on("users");
$table->softDeletes();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists("addresses");
}
}
Well you miss $table that you pass to the function.
Your schema create function needs to be in this style...
Schema::create('addresses', function(Blueprint $table)
I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.
I have created a table with columns using php artisan and entered some test data. I now want to add a few new columns to the database without affecting the current data, and setting the new fields to be null.
My inital thought was to enter a new field in the database migrate file and the run "php artisan migrate", but this just gave me the message "nothing to migrate" and did enter the new column in my database.
Here is my database migrate file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateFestivalsTable extends Migration {
public function up()
{
Schema::create('festivals', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title');
$table->timestamps();
});
}
public function down()
{
Schema::drop('festivals');
}
}
create new migration with artisan name it addColumnFestivalTable
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class addColumnFestivalTable extends Migration {
public function up()
{
Schema::table('festivals', function($table)
{
$table->string('new_col_name');
});
}
public function down()
{
Schema::table('festivals', function($table)
{
$table->dropColumn('new_col_name');
});
}
}
for more information read Laravel 5.4 doc