Update table and add data in a Laravel 5 Migration

I need to add a new column in my laravel Project, no problem for this, I used the Schema::table() to update and it's ok. Now I need to find out how many records I have on this table and update with some value.

I have the table Warrants:

Schema::create('warrant_grants', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('warrant_plan_id');
    $table->integer('shareholder_id');
});

So I created the new field with a new migration file:

Schema::table('warrant_grants',function ($table) {
    $table->string('name',100);
});

Now I need to update this field name in the table with some values, for example if the table has 100 records, then I need to insert in every row the value "Warrant-X" where X is a number starting with 1 to 100. For example:

Warrant-1, Warrant-2, ....Warrant-100.

I spent hours looking for some way to do this using Seeds but I didn't found. So basically i have two questions:

  • Can I use Seeds in Laravel 5 to update values or I can just insert them?
  • Can I create some SQL inside the Seeds (or migrations) to do this update for me?

Based on this link i found the answer: https://stackoverflow.com/a/23506744/4650792

Schema::table('warrant_grants',function ($table){
        $table->string('name',100)->after('id')->nullable();
    });

    $results = DB::table('warrant_grants')->select('id','name')->get();

    $i = 1;
    foreach ($results as $result){
        DB::table('warrant_grants')
            ->where('id',$result->id)
            ->update([
                "name" => "Warrant-".$i
        ]);
        $i++;
    }

Thanks for the help anyway guys.

In Brief #002: Updating Rows In A Migration, In Brief #002: Updating Rows In A Migration. If your column will allow a default value, you can add it as part of the migration itself $table->string('colName')->default('foo'); You can use a Seeder to insert rows, or to run just about any query you'd like. You can add DB code to your migration. If it is added to the migrations table already, it wont process it again. Your solution is to create a new migration, for which you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models. for Laravel 5+: php artisan make:migration add_paid_to_users_table --table=users


Yes, you can perform updates/inserts/whatever in your migrations. For example:

Schema::table('warrant_grants', function($table) {
    $table->string('name', 100);
});

$i = 1;
foreach (WarrantGrants::all() as $warrant_grant) {
    $warrant_grant->update([
      'name' => 'Warrant-' . $i
    ]);

    $i++;
}

Database: Migrations - Laravel, php artisan migrate:rollback --step=5 The migrate:fresh command will drop all tables from the database and then execute the migrate command: The table method on the Schema facade may be used to update existing tables. and a Closure that receives a Blueprint instance you may use to add columns to the table: Sometimes you need to add a new column to a table that already exists, for this we can use laravel migrations. In our command line we type (You can name the migration file as you want ) : php artisan migrate: make add_field_to_table. In line 14 we're adding a new field (table column) into our table. We type the field type if you don't know the field types you can find them in the


Other answers are correct. But note that if you have a lot of records, updating all of them with ORM can take time. Use raw SQL queries to do that faster.

Schema::table('warrant_grants',function ($table){
        $table->string('name',100)->after('id')->nullable();
    });
DB::raw("UPDATE warrant_grants SET name=name+id");

The SQL query is not exact, and you have to make it for your own DB, but you get the point.

Migrations & Seeding - Laravel, php artisan make:migration add_votes_to_users_table --table=users php The up method is used to add new tables, columns, or indexes to your database, may use the Laravel schema builder to expressively create and modify tables. $table->decimal('amount', 5, 2);, DECIMAL equivalent with a precision and scale. After migration and having data into the table, now maybe you want to add a new column to the users table called profile. To add a new column, refresh database is not an ideal way. In this situation, you can create a new migration file to add a new column into the users table. Add a single Column: To add a new column to the existing table in Laravel, you need to add the following command in terminal. php artisan make:migration add_profile_to_users. It will create a add_profile_to_users file


Another possible syntax to achieve this:

DB::table('warrant_grants')
   ->where('id',$result->id)
   ->update([
      "name" => DB::raw("'Warrant-' + `name`")
   ]);

This allows the update to be done as one batch rather than iterating over results, and retains most of the familiar Eloquent syntax rather than falling back to just using raw SQL.

The string concatenation syntax may need to be changed depending on the SQL variant used.

Schema Builder - Laravel, Introduction; Creating & Dropping Tables; Adding Columns; Changing Columns; Renaming To create a new database table, the Schema::create method is used : To update an existing table, we will use the Schema::table method: $table-> decimal('amount', 5, 2);, DECIMAL equivalent with a precision and scale. Add a single Column an existing table; Add Multiple Columns in Existing Table; Available Column Types; Let’s have a table called notes where the table you want to add notes type. Add a single Column an existing table. To add a new column to the existing table using the laravel migration. Let’s open your terminal and create a migration file


How to overwrite/update already migrated table with new columns, I want to update my database table with migration method without affecting the records. 1 of 10 replies September 13, 2016. anilvarey's avatar anilvarey � Laravel � anilvarey •. 3 years ago I want to add more columns to my table structure (not from phpmyadmin). Posted 3 years ago by @jacobrossdev 5 months ago :P. The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Generating Migrations. To create a migration, use the make:migration Artisan command: php artisan make:migration create_users_table. The new migration will be placed in your database/migrations


How to add a new column to existing table of laravel in a migration , For Laravel 5+: php artisan make:migration add_paid_to_users_table --table= users. You then need to use the Schema::table() method (as� The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.


How to add new columns to the existing table in Laravel migration, After migration and having data into the table, now maybe you want to add a new column to the users table called profile . To add a new column, refresh database � The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Generating Migrations. To create a migration, use the make:migration Artisan command: php artisan make:migration create_users_table. The new migration will be placed in your database/migrations