如何解决通过 Laravel 队列插入数据工作
我有两个表,分别称为库存 和产品。在 Inventory->inventory_id 和 Product->product_code 中,并且两个表中的 'sku' 字段都相同,一些 Inventory->sku 字段为空。我想从 Product->sku 插入所有 Inventory->sku,其中 Inventory->inventory_id = Product->product_code。
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Inventory;
use App\Product;
use App\Jobs\AddProductQueue;
class InventoryController extends Controller
{
public function createSku(Request $request){
$product = Product::where("product_code",$request->product_code)->first();
AddProductQueue::dispatch($product);
}
}
工作
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
use App\Product;
use App\Inventory;
class AddProductQueue implements ShouldQueue
{
use Dispatchable,InteractsWithQueue,Queueable,SerializesModels;
protected $product;
public function __construct(Product $product)
{
$this->product = $product;
}
public function handle()
{
try{
$inventory = Inventory::where("inventory_id",$this->$product->product_code)->first();
$product = Product::where("product_code",$this->$product->product_code)->first();
if($inventory->sku != null){
return response()->json([
"status" => 0,"messege" => "Bar Code Field isn't Empty"
]);
}else{
$inventory->sku = $product->sku;
$inventory->save();
return response()->json([
"status" => 1,"messege" => "Success"]);
}
}catch (Throwable $e) {
report($e);
return false;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。