【问题标题】:Insert data into table using queue使用队列将数据插入表中
【发布时间】:2019-05-17 05:49:48
【问题描述】:

我正在使用 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 表中。

【问题讨论】:

    标签: php database laravel laravel-4


    【解决方案1】:

    您的队列函数没有保存任何数据,它只是试图检索数据。

    您需要明确保存数据。

    将您的查询更改为:

    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

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多