我试图在后台的数据库表中创建多个行,以减少页面加载时间,因此我为此实现了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);
}
}
}
我希望在后台创建数据库行
答案 0 :(得分:0)
请更改您的工作类别
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);
}
}