⚝
One Hat Cyber Team
⚝
Your IP:
216.73.216.91
Server IP:
157.245.101.34
Server:
Linux skvinfotech-website 5.4.0-131-generic #147-Ubuntu SMP Fri Oct 14 17:07:22 UTC 2022 x86_64
Server Software:
Apache/2.4.41 (Ubuntu)
PHP Version:
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
var
/
www
/
html
/
app
/
Imports
/
View File Name :
OrdersImport.php
<?php namespace App\Imports; use Illuminate\Support\Collection; use App\Models\Order; use App\Models\OrderProduct; use App\Models\Product; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; use Illuminate\Support\Facades\Validator; use Maatwebsite\Excel\Concerns\WithValidation; class OrdersImport implements ToCollection,WithHeadingRow { /** * @param Collection $collection */ public function __construct($type,$address,$orderid){ $this->type = $type; $this->address = $address; $this->order_id = $orderid; } public function collection(Collection $rows) { $validator = Validator::make($rows->toArray(),[ '*.product_name' => 'required', '*.quantity' => 'required|integer', ]); if($validator->fails()){ return session(['order_import_error' => "The Product name and quantity is required,Please check your field and try again"]); } $user = request()->user(); $quantity = $rows->sum('quantity'); foreach($rows as $row) { $product = Product::where('product_name',$row['product_name'])->where('type',$this->type)->first(); $order_product = new OrderProduct(); if($product){ $order_product->product_id = $product->id; }else{ $other = Product::where('product_name','others')->where('type',$this->type)->first(); if($other){ $new_other_product = new Product(); $new_other_product->product_name = $row['product_name']; $new_other_product->type = $this->type; $new_other_product->other_id = $other->id; $new_other_product->save(); $order_product->product_id = $new_other_product->id; }else{ $order = Order::find($this->order_id); $order->delete(); return session(['order_import_error' => "The Product does not match"]); } } $order_product->order_id = $this->order_id; $order_product->user_id = $user->id; $order_product->quantity = $row['quantity']; $order_product->save(); // $order->orderedProducts()->save($order_product); } // dd($quantity); return session(['order_import_success' => "success",'quantity' => (int)$quantity]); } }