From 632a2b3c809b50292e7cc970bc493b14960ac8ca Mon Sep 17 00:00:00 2001 From: raccoon254 Date: Thu, 20 Jun 2024 22:39:41 +0300 Subject: [PATCH] Order delivery create components --- app/Livewire/OrderDelivery.php | 53 ++++++++++++++++++- .../views/livewire/order-delivery.blade.php | 28 +++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/app/Livewire/OrderDelivery.php b/app/Livewire/OrderDelivery.php index cb2c332..e733549 100644 --- a/app/Livewire/OrderDelivery.php +++ b/app/Livewire/OrderDelivery.php @@ -2,11 +2,62 @@ namespace App\Livewire; +use App\Models\Order; +use App\Models\Delivery; +use App\Models\DeliveryAttachment; +use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Redirect; +use Illuminate\View\View; use Livewire\Component; +use Livewire\Features\SupportRedirects\Redirector; +use Livewire\WithFileUploads; class OrderDelivery extends Component { - public function render() + use WithFileUploads; + + public Order $order; + public $status; + public $description; + public $attachments = []; + + protected $rules = [ + 'status' => 'required|string|max:255', + 'description' => 'nullable|string', + 'attachments.*' => 'file|max:10240', // max 10MB per file + ]; + + public function mount(Order $order): void + { + $this->order = $order; + } + + public function saveDelivery(): RedirectResponse | Redirect | Redirector + { + $this->validate(); + + $delivery = Delivery::create([ + 'order_id' => $this->order->id, + 'status' => $this->status, + 'description' => $this->description, + ]); + + foreach ($this->attachments as $attachment) { + $path = $attachment->store('deliveries'); + DeliveryAttachment::create([ + 'delivery_id' => $delivery->id, + 'file_path' => $path, + 'file_name' => $attachment->getClientOriginalName(), + 'file_type' => $attachment->getClientMimeType(), + ]); + } + + session()->flash('success', 'Delivery created successfully.'); + + return redirect()->route('orders.show', $this->order); + } + + public function render(): View { return view('livewire.order-delivery'); } diff --git a/resources/views/livewire/order-delivery.blade.php b/resources/views/livewire/order-delivery.blade.php index a40248d..17338aa 100644 --- a/resources/views/livewire/order-delivery.blade.php +++ b/resources/views/livewire/order-delivery.blade.php @@ -1,3 +1,29 @@
- {{-- Knowing others is intelligence; knowing yourself is true wisdom. --}} +
+
+ + + @error('status') {{ $message }} @enderror +
+ +
+ + + @error('description') {{ $message }} @enderror +
+ +
+ + + @error('attachments.*') {{ $message }} @enderror +
+ + +
+ + @if (session()->has('success')) +
+ {{ session('success') }} +
+ @endif