Функция Job просто продолжает обработку без обработки - PullRequest
0 голосов
/ 15 апреля 2019

Я пытаюсь создать несколько строк в таблице базы данных за сценой, чтобы уменьшить время загрузки страницы, поэтому я реализую очередь laravel для этого. Но реальная работа, кажется, не выполняется без ошибок

это у меня в контроллере

public function store(SlotRequest $request)
{
    $quota = 2;
    $slotquota = request('slotamount') + $quota;

    if ( auth()->user()->wallet->balance < $slotquota ) {

        return Redirect::back()->with('low_balance', 'You do not have a sufficient wallet balance to reserve these SLOTS. Please Load Up Your Wallet');

    } else {

        // Getting SLOTS as objects of an array
        $slotquantity = new SplFixedArray(request('slotamount'));
        $slotquantity = $slotquantity->toArray();
        $user = auth()->user();

        SlotQueuer::dispatch($slotquantity, $user);

    }

        //Sorting Wallet Balance
        $wallet = Wallet::where('user_id', auth()->user()->id)->first();
        $wallet->balance = $wallet->balance - $slotquota;
        $wallet->save();



        //Returning View With Message
        return Redirect::back()->with('reserved', 'Your SLOTS have been successfully reserved');
}

и за мою работу

namespace App\Jobs;

use App\Events\SlotCounter;
use App\Slot;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SlotQueuer implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $slotquantity;
    protected $user;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

       // Inserting Rows in SLOTS Table
       foreach ($this->slotquantity as $slot) {

        $slot = new Slot();

        $slot->user_id = $this->user->id;

        $slot->save();

        //Slot Counting Event
        event(new SlotCounter);

        }

    }
}

Я надеюсь создать строки базы данных за кулисами

1 Ответ

0 голосов
/ 15 апреля 2019

Пожалуйста, измените свой класс работы

protected $slotquantity;
protected $user;

public function __construct($slotquantity , $user)
{
 $this->slotquantity = $slotquantity;
 $this->user = $user;
}

В вашей функции handle ()

public function handle()
{

 // Inserting Rows in SLOTS Table
 foreach ($this->slotquantity as $slot) { //use this keyword to access slotquantity

 $slot = new Slot();

 $slot->user_id = $this->user->id;

 $slot->save();

 //Slot Counting Event
  event(new SlotCounter);

 }

 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...