How to merge two table data in laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Merge Two Table Data in Laravel: The Right Way to Combine Your Data
As a senior developer working with the Laravel ecosystem, you constantly encounter the need to combine data from different sources. Whether you are dealing with relational data or simply needing a unified list of records, knowing how to merge tables efficiently is crucial for writing scalable and performant applications.
The scenario you presented—trying to use a method like merge() on two separate database result sets (table1 and table2)—highlights a common misunderstanding of how relational data should be combined in SQL and Laravel. While the Query Builder offers powerful tools, direct merging often leads to incorrect results or severe performance bottlenecks.
This post will walk you through the correct, developer-focused ways to merge data in Laravel, focusing on the two most common scenarios: relational merging (JOINs) and set merging (UNIONs).
Why Direct Merging Fails: Understanding Database Relationships
When you attempt to use methods like merge() directly on separate query results, you are essentially performing a simple collection merge in PHP after the data has already been retrieved from the database. This approach is inefficient and often logically flawed because it doesn't respect the underlying relationships defined in your database schema.
For combining tables based on shared keys (like matching user IDs to order details), the correct operation belongs squarely within the SQL layer, where operations are optimized for speed. The most efficient way to achieve this is by using SQL JOINs.
Solution 1: The Relational Approach – Using SQL JOINs
If table1 and table2 are related (e.g., users placed orders), you should use an INNER JOIN, LEFT JOIN, or RIGHT JOIN. This tells the database to look at both tables simultaneously and return only the rows where the conditions match, performing the merge operation directly on the server side.
Let’s assume table1 has the user data and table2 has the order data, and they share a common field (e.g., an email or a foreign key link).
Example using Laravel Query Builder:
If you want to retrieve all users along with their corresponding orders:
use Illuminate\Support\Facades\DB;
$mergedData = DB::table('table1')
->join('table2', 'table1.email', '=', 'table2.email') // Join condition based on matching email
->select(
'table1.*', // Select all columns from table1
'table2.mobile' // Select the mobile number from table2
)
->orderBy('table1.id')
->get();
// $mergedData now contains a single, unified result set containing data from both tables, linked by the join condition.
dd($mergedData);
Best Practice Note: When dealing with complex relationships in Laravel, leveraging Eloquent Models and their defined relationships (e.g., defining hasMany or belongsTo) is far superior to writing raw JOIN syntax. This keeps your data logic clean and adheres to the principles of robust application design championed by frameworks like Laravel.
Solution 2: The Set Approach – Using UNION
If your goal is not to link related records but rather to create one large, unified list containing all records from both tables (e.g., a master list of all user entries and all order entries), you should use the UNION or UNION ALL operator in your query. This combines the results vertically into a single result set.
Example using Laravel Query Builder:
$combinedData = DB::table('table1')
->select('id', 'name', 'phone', 'email', 'status', 'created_at', 'source')
->where('source', 'table1') // Labeling the source for clarity
->unionAll(
DB::table('table2')
->select('id', 'name', 'mobile', 'email', 'status', 'created_at')
->where('source', 'table2') // Labeling the source for clarity
);
dd($combinedData);
The UNION ALL operator is generally preferred over UNION because it is faster, as it does not check for duplicate rows (unless explicitly requested), making it ideal for combining large datasets.
Conclusion: Choose the Right Tool
To summarize, when merging table data in Laravel, always prioritize performing the merge operation within the database layer using native SQL constructs:
- For Linked Data (Relationships): Use **
JOIN**s to combine related rows horizontally into one record set. - For Unrelated Data (Set Combination): Use
UNION ALLto stack rows vertically into a single list.
By relying on these powerful database operations rather than attempting PHP-level merging, you ensure your application is fast, scalable, and adheres to the principles of efficient data handling that modern frameworks like Laravel promote. Mastering these tools is key to unlocking the full potential of working with relational data in PHP.