簡體   English   中英

laravel雄辯地將數據從一個表插入到另一個表

[英]insert data from one table to another with laravel eloquent

我有兩個表,一個表的主鍵引用了另一個表的列。 現在,我需要將所有主鍵復制到第二張表。

我有以下兩種模型:

class Products extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;


    protected $connection = 'mysql3';

    protected static $_table;

    public function setTable($table)
    {
        static::$_table = $table;
    }

    public function getTable()
    {
        return static::$_table;
    }

    public function skuEAN() {

        return $this->hasOne('SkutoEAN', 'seller_sku', 'seller_sku');


    }

class SkutoEAN extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;


    protected $connection = 'mysql3';

    protected static $_table;

    public function setTable($table)
    {
        static::$_table = $table;
    }

    public function getTable()
    {
        return static::$_table;
    }

    protected $fillable = ['seller_sku','EAN','fallback'];

    public function connectToproducts() {

        return $this->belongsTo('de_products');

    }

現在在我的控制器中,我正在這樣做:

$data = Products::get(['seller_sku']);
    foreach ($data as $a) {
        $skuean = SkutoEAN::create(['seller_sku' => $a->seller_sku]);
        $skuean->save();
}

可以,但是大約需要3分鍾才能復制2500個條目,這可能不正確。 有沒有一種方法可以雄辯地直接復制數據,而無需先將數據存儲在內存中?

您可以只使用Eloquent::insert() 例如:

$data = array(
    array('name'=>'Coder 1', 'rep'=>'4096'),
    array('name'=>'Coder 2', 'rep'=>'2048'),
    //...
);

Coder::insert($data);

也許這個問題可以幫助您!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM