插入选择 Laravel Eloquent

huangapple go评论65阅读模式
英文:

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:

&gt; 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&#39;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(&#39;connection2&#39;)
    -&gt;table(&#39;view_tipo_cliente&#39;)
    -&gt;select(&#39;id_prospecto&#39;, &#39;cedula&#39;, &#39;plan&#39;, &#39;tipo_cliente&#39;)
    -&gt;each(function ($row) {
        DB::table(&#39;crm_tipo_cliente&#39;)-&gt;insert([
            &#39;id_prospecto&#39; =&gt; $row-&gt;id_prospecto,
            &#39;cedula&#39; =&gt; $row-&gt;cedula,
            &#39;plan&#39; =&gt; $row-&gt;plan,
            &#39;tipo_cliente&#39; =&gt; $row-&gt;tipo_cliente,
        ]);
    });

huangapple
  • 本文由 发表于 2023年6月15日 21:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定