繁体   English   中英

队列 Laravel 中的更新查询不起作用

[英]Doesnt work update query in queue Laravel

在 controller 中,我需要在 24 小时后更新用户余额,我将产品价格和卖家 ID 发送到队列,然后我尝试按队列更新用户余额,但队列触发没有错误,并且数据库中的余额不会改变. 我该如何解决这个问题?

wd

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;

class CreditMoney implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;


    protected $price;

    protected $seller_id;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($price, $seller_id)
    {
        $this->$price = $price;
        $this->$seller_id = $seller_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $manySellery = $this->price - ($this->price * (5 / 100));
        $balance = User::where('id', $this->seller_id)->value('balance');
        $money = $balance + $manySellery;
        return User::where(['id' => $this->seller_id])->update(array('balance' => $money));
    }
}
public function confirm($id)
    {
        $data = Product::where('id', $id)->select(array('price', 'dispute', 'seller_id', 'is_confirmed'))->first();
        if ($data->is_confirmed && !is_null($data->dispute)) return redirect()->to(route('index'));

        Product::where('id', $id)->update(array('is_confirmed' => 1));
        
        dispatch(new CreditMoney($data->price,$data->seller_id));
        
        return redirect()->to(route('order', $id))->with('confirm', 'Confirmed.');
    }

尝试:

        return User::where('id', $this->seller_id)->update(array('balance' => $money));

使用此代码段

return User::where('id', $this->seller_id)->update(['balance' => $money]);

在您的User Model 中,检查可填充数组。 看看您是否在该数组中添加了balance

如果将列添加到fillable属性,则更新方法仅更新 DB 中的值。

这个错误非常愚蠢,我写的是“$this->$price”而不是“$this->price”。 对不起

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM