Level 122
You are doing this from a get route? Your browser might pre-fetch the route causing it to be hit twice.
Instead of a link, submit a form to a post route.
Hi all,
I have a que that runs to help upload categories or products to my WP instance. I have noticed that the Job runs twice. I have tried running it as it's own function to confirm if it was my code or something else. It added the job twice in the database. It then fails the second time as the categories are found to already be loaded.
Route
Route::match(['get', 'post'], 'woocommerce/products/migrate', 'Backend\ProductController@wooMigrate')->name('wooMigrate');
VIew
<div class="col-md-6 text-center">
<h4>Esquire Shop to WooCommerce</h4>
<div class="card py-4">
<div class="pt-3">
<img src="{{asset('backend/images/suppliers/esquire.jpg')}}" alt="esquire" height="100px">
<i class="feather-64 p-2" data-feather="chevrons-right"></i>
<img src="{{asset('backend/images/suppliers/woo.png')}}" alt="woocommerce" height="100px">
</div>
<div class="pt-4">
<a href="" onclick="if(confirm('Are you sure you want to migrate Esquire Products to your WordPress site?')){event.preventDefault();document.getElementById('migrate-esquire').submit();}else{event.preventDefault();}" class="btn btn-primary">Migrate Products</a>
<form id="migrate-esquire" method="post" action="{{route('wooMigrate')}}" style="display:none">
@csrf
<input type="hidden" name="type" value="esquire">
</form>
<a href="" onclick="if(confirm('Are you sure you want to migrate Esquire Categories to your WordPress site?')){event.preventDefault();document.getElementById('migrate-esquire-cat').submit();}else{event.preventDefault();}" class="btn btn-dark">Migrate Categories</a>
<form id="migrate-esquire-cat" method="post" action="{{route('wooMigrate')}}" style="display:none">
@csrf
<input type="hidden" name="type" value="esquire-cat">
</form>
</div>
</div>
</div>
Controller
public function wooMigrate(Request $request) {
if($request->type == 'esquire') {
// Run Que Job
$esquireProductMigrate = new EsquireProductMigrate;
$this->dispatch($esquireProductMigrate);
}
if($request->type == 'esquire-cat') {
// Run Que Job
$esquireCategoriesMigrate = new EsquireCategoriesMigrate;
$this->dispatch($esquireCategoriesMigrate);
}
if($request->type == 'thermalgrizzly-za') {
//
}
return view('admin.woocommerce.products.migrate');
}
Job
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 Codexshaper\WooCommerce\Facades\Category;
class EsquireCategoriesMigrate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$categories = \App\Models\Product::all();
$categories = $categories->unique('category');
foreach($categories as $cat) {
$data = [
'name' => $cat->category,
'image' => [
'src' => ''
]
];
Category::create($data);
}
}
}
Result in Console
PS C:\Program Files\Ampps\www\cyberwareLara> php artisan queue:work
[2021-11-19 19:06:03][1] Processing: App\Jobs\EsquireCategoriesMigrate
[2021-11-19 19:15:44][1] Processed: App\Jobs\EsquireCategoriesMigrate
[2021-11-19 19:15:44][2] Processing: App\Jobs\EsquireCategoriesMigrate
[2021-11-19 19:15:46][2] Failed: App\Jobs\EsquireCategoriesMigrate
Please or to participate in this conversation.