Home > Mobile >  Doesnt work update query in queue Laravel
Doesnt work update query in queue Laravel

Time:08-03

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. How can i solve this?

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

CodePudding user response:

try:

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

CodePudding user response:

use this code snippet

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

CodePudding user response:

Inside your User Model, check the fillable array. and see if you have added the balance in that array.

Update method only updates the values in DB if the column is added to fillable attributes.

CodePudding user response:

The error was pretty stupid, I wrote "$this->$price" instead of "$this->price". Sorry

  • Related