使用队列将数据插入表

时间:2018-12-16 18:55:16

标签: php database laravel laravel-4

我正在使用Laravel队列功能将数据自动插入数据库表中。如果表中没有记录,数据将被自动保存。我的第一步是能够将数据插入表中,然后仅进行验证。但是,我的数据似乎无法插入数据库。

我的队列功能

public function save_sale_price_on_the_fly($job, $data)
    {
        $selling_price = array_get($data, 'price');
        $item_id = array_get($data, 'item_id');
        $payee_id = array_get($data, 'payee_id');

            DB::table('customer_products')
            ->where('product_id', '=', $item_id)
            ->where('customer_id', '=', $payee_id)
            ->where('different_price', '=', $selling_price)
            ->first();
    }

我的模特

static::saved(function($model)
        {
            if (!$model->item_id && $model->payee_id && $model->price) {
                Queue::push('InvoiceQueue@save_sale_price_on_the_fly', $model->toArray());
            }
        });

该项目,收款人和价格是从另一个表返回的。我正在尝试将此3值传递到我的customer_products表中。

1 个答案:

答案 0 :(得分:0)

您的队列功能不保存任何数据,而只是尝试检索数据。

您需要显式保存数据。

将查询更改为以下内容:

DB::table('customer_products')
    ->insert([
        'product_id' => $item_id,
        'customer_id' => $payee_id,
        'different_price' => $selling_price
     ]);

Laravel 4文档在这里:https://laravel.com/docs/4.2/queries#inserts