英文:
Insert Select Laravel Eloquent
问题
我有以下问题。
我尝试做这个:
DB::table('crm_client_type')
->insertSelect(
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'id', 'plan', 'tipo_cliente')
);
然而,它告诉我:
> insertSelect 在 QueryBuilder 中可用
我需要做的是执行一个插入选择操作,但要知道其中一个表位于另一个连接(服务器)中,我不能使用类似于 FOR 或 FOREACH 的块,因为服务器会挂起。
DB::table('crm_tipo_cliente')
->insertSelect(
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'cedula', 'plan', 'tipo_cliente')
);
<details>
<summary>英文:</summary>
I have the following problem.
I tried to do this:
DB::table('crm_client_type')
->insertSelect(
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'id', 'plan', 'tipo_cliente')
);
however, it told me that:
> insertSelect is available in QueryBuilder
What I need is to do an insert select somehow knowing that one of the tables is located in another connection (server) and I can't do blocks like FOR or FOREACH because the server hangs
DB::table('crm_tipo_cliente')
->insertSelect(
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'cedula', 'plan', 'tipo_cliente')
);
</details>
# 答案1
**得分**: 1
insertSelect方法在Laravel的QueryBuilder中默认不可用。尝试使用以下代码:
```php
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'cedula', 'plan', 'tipo_cliente')
->each(function ($row) {
DB::table('crm_tipo_cliente')->insert([
'id_prospecto' => $row->id_prospecto,
'cedula' => $row->cedula,
'plan' => $row->plan,
'tipo_cliente' => $row->tipo_cliente,
]);
});
英文:
The insertSelect method is not available in Laravel's QueryBuilder by default. Try it
DB::connection('connection2')
->table('view_tipo_cliente')
->select('id_prospecto', 'cedula', 'plan', 'tipo_cliente')
->each(function ($row) {
DB::table('crm_tipo_cliente')->insert([
'id_prospecto' => $row->id_prospecto,
'cedula' => $row->cedula,
'plan' => $row->plan,
'tipo_cliente' => $row->tipo_cliente,
]);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论