@lottie
To insert data from one table to another in Laravel, you can use Eloquent ORM or Query Builder. Here are two methods to insert data from one table to another:
Method 1: Using Eloquent ORM
Example:
1 2 3 4 5 6 7 8 |
$records = ModelA::select('column1', 'column2')->get(); foreach ($records as $record) { ModelB::create([ 'column1' => $record->column1, 'column2' => $record->column2, ]); } |
Method 2: Using Query Builder
Example:
1 2 3 4 5 6 7 8 |
$records = DB::table('table_a')->select('column1', 'column2')->get(); foreach ($records as $record) { DB::table('table_b')->insert([ 'column1' => $record->column1, 'column2' => $record->column2, ]); } |
After executing the above code, the data from Table A will be inserted into Table B. Make sure to adjust the column names and table names in the examples based on your database structure.