简体   繁体   English

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

[英]Doesnt work update query in queue Laravel

In controller i need to update user's balance after 24hours,i send price of product and seller id to queue, then i'm trying to update user balance by queue, but the queue fires without error's, and the balance in the database does not change.在 controller 中,我需要在 24 小时后更新用户余额,我将产品价格和卖家 ID 发送到队列,然后我尝试按队列更新用户余额,但队列触发没有错误,并且数据库中的余额不会改变. How can i solve this?我该如何解决这个问题?

wd 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.');
    }

try:尝试:

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

use this code snippet使用此代码段

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

Inside your User Model, check the fillable array.在您的User Model 中,检查可填充数组。 and see if you have added the balance in that array.看看您是否在该数组中添加了balance

Update method only updates the values in DB if the column is added to fillable attributes.如果将列添加到fillable属性,则更新方法仅更新 DB 中的值。

The error was pretty stupid, I wrote "$this->$price" instead of "$this->price".这个错误非常愚蠢,我写的是“$this->$price”而不是“$this->price”。 Sorry对不起

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

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