Laravel update database record running script
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Database Updates in Laravel: Migrations vs. Raw SQL
As developers working with Laravel, we often need to interact directly with the database—not just for application logic, but sometimes to execute complex, one-off data manipulations or structural changes. The core question is: when I have a specific SQL update, what is the most idiomatic and maintainable way to achieve it within the Laravel ecosystem? Should I rely on Seeds, Migrations, or Factories?
This post will dissect the best architectural approach for updating database records in Laravel, moving from the declarative structure of migrations to the practical execution of data updates.
Understanding the Laravel Database Hierarchy
Laravel provides a powerful set of tools designed to manage the lifecycle of your database schema and data. Understanding the separation of concerns between these tools is crucial before executing any script.
1. Migrations: The Blueprint for Structure
Migrations are the foundational tool for defining schema changes. They act as version control for your database structure. When you run a migration, you are telling Laravel exactly how the table should look (creating columns, adding indexes, setting constraints).
For example, if you needed to add a new column named meta_desc to your translations table:
// database/migrations/..._create_translations_table.php
Schema::table('translations', function (Blueprint $table) {
$table->string('meta_desc')->nullable();
});
Migrations are perfect for setting up the initial state or defining the rules of your data structure. They ensure that every environment (local, staging, production) has the exact same table structure.
2. Seeders and Factories: Populating the Data
Seeders and Factories handle the population of that defined structure with initial or test data. Factories allow you to define how records should look, and Seeders run those factories to insert the actual rows into the database. They are ideal for creating complex sets of test data, not for performing arbitrary, operational updates on existing records.
3. Eloquent and the Query Builder: Executing Data Updates
When your goal is to perform an actual update on existing data—like the example you provided (UPDATE translations SET field = 'meta_desc' WHERE field = 'page_desc')—relying solely on migrations or factories is inefficient. This type of operation belongs in your application logic, where it can be conditional and context-aware.
For safe and expressive updates, we should leverage Eloquent ORM or the Query Builder rather than dropping into raw SQL directly everywhere.
Executing the Specific Update Example
Let's look at how to handle an update like: UPDATE translations SET field = 'meta_desc' WHERE field = 'page_desc' using Laravel best practices.
The Eloquent Approach (Recommended)
If you are updating records based on conditions, using Eloquent provides much better readability and security than raw SQL strings. Before running the update, you must first retrieve the records you intend to modify:
use App\Models\Translation;
// Find all records where the condition is met
$translationsToUpdate = Translation::where('field', 'page_desc')->get();
foreach ($translationsToUpdate as $translation) {
// Apply the desired change using Eloquent update methods
$translation->field = 'meta_desc';
$translation->save();
}
This method is superior because it leverages Laravel's relationships, handles potential database errors gracefully, and keeps your data manipulation within the object-oriented structure of the framework. This approach aligns perfectly with the principles of clean development advocated by the Laravel community.
When to Use Raw SQL (DB::statement)
While Eloquent is preferred for business logic, there are rare scenarios where raw SQL execution is unavoidable—such as executing highly complex stored procedures or bulk operations that are optimized only within the database engine itself.
If you must execute a direct UPDATE statement based on your example:
use Illuminate\Support\Facades\DB;
$result = DB::update(
'UPDATE translations SET field = ? WHERE field = ?',
['meta_desc', 'page_desc']
);
// $result will contain the number of affected rows.
A Word of Caution: Always sanitize and parameterize raw SQL queries when dealing with external inputs or complex logic. Using prepared statements (as shown above) is critical to prevent SQL injection vulnerabilities, a core security tenet emphasized by Laravel.
Conclusion: The Right Tool for the Job
To summarize, there is no single "best" solution; there is only the most appropriate solution:
- Migrations: Use these exclusively for defining and evolving your database structure.
- Eloquent/Query Builder: Use these for all application-level data manipulation, making updates expressive, secure, and maintainable. This is the Laravel way.
- Raw SQL (
DB::update): Reserve this for highly complex bulk operations where performance dictates direct database interaction, always ensuring proper parameter binding to mitigate security risks.
By respecting this hierarchy, you build robust applications that are both powerful and secure.