Update table and add data in a Laravel 5 Migration
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
When working on your Laravel 5 project, you may come across the need to update an existing table by adding a new column or even populating it with data. This process can be smoothly accomplished using Laravel's Migration system and some clever coding techniques. In this post, we will discuss how to use Laravel's powerful features to make this task efficient and straightforward.
Creating a New Column Using the Schema Class
Firstly, as you have already added the new column name in your Warrant_grants table, we won't discuss creating columns here. If you need to add one, you can use Laravel Migrations, as illustrated in the code snippet below:
Schema::create('warrant_grants', function(Blueprint $table) {
$table->increments('id');
$table->integer('warrant_plan_id');
$table->integer('shareholder_id');
$table->string('name',100); // Add the new column here.
});
Updating Values in Laravel Migrations
Now, let's discuss your primary concern: updating existing records with values. You can leverage Laravel's Eloquent ORM (Object Relation Mapper) to perform this task. Firstly, you need a migration file that updates the table structure and populates data. Let's call it "2018_03_01_164715_update_warrant_grants_table".
Using Eloquent Model for Updates
// Migration file: 2018_03_01_164715_update_warrant_grants_table.php
use Illuminate\Support\Facades\Schema;
use App\Models\WarrantGrants; // Import your Model here.
public function up() {
Schema::create('warrant_grants', function(Blueprint $table) {
// Add existing column definitions.
});
WarrantGrants::truncate(); // Clear the table to repopulate data.
for ($i = 1; $i <= $this->warrantCount; $i++) {
$warrant = new WarrantGrants();
$warrant->name = "Warrant-{$i}"; // Create and assign the value.
$warrant->saveOrFail();
}
}
This code creates the table with existing columns, truncates (clears) the table's data, iterates through a defined range to generate unique names for your warrants, creates and saves new records with these names, and finally saves it into the database. In this case, you can set $this->warrantCount to match the number of records you want to generate.
Updating Values Using Seeds
Seeders are also an alternative method for populating your data. A seeder is a class that is used primarily during the testing, staging, and deployment phases to ensure the necessary data is always present in the database. You can leverage this by creating a new seeder file named "WarrantGrantsTableSeeder" with the following content:
// Seed file: WarrantGrantsTableSeeder.php
use App\Models\WarrantGrants; // Import your Model here.
use Faker\Factory as Faker;
use Illuminate\Database\Eloquent\Model;
class WarrantGrantsTableSeeder extends BaseSeeder {
/**
* Run the database seeds.
*/
public function run() {
Model::unguard(); // Prevents mass assignment protection while seeding.
WarrantGrants::truncate(); // Clear the table to repopulate data.
for ($i = 1; $i <= $this->warrantCount; $i++) {
$warrant = new WarrantGrants();
$warrant->name = "Warrant-{$i}"; // Create and assign the value.
$warrant->saveOrFail();
}
}
}
This code essentially performs the same task as mentioned in the previous example, but you can create it using seeds instead of migrations. It also ensures that mass assignment protection is disabled while running the seeding process without explicitly calling Model::disableForeignKeyConstraints() or Model::unguard(). This method is usually safer and more common in modern Laravel applications.
Conclusion
Both approaches can be utilized to update data or insert new values into a table. The best choice depends on your project's needs, the nature of your database tables, and the overall structure of your application. Regardless of which method you choose, always ensure that you employ good practices and security measures to prevent any possible issues with your database data.
Note: In Laravel 5, Seeds may only be used for creating and populating tables, not modifying existing records. However, in later versions of Laravel (6+), you can use Artisan's db:seed --class command to run a specific seeder class.