Skip to content

Commit

Permalink
Order delivery create components
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 20, 2024
1 parent fd89dad commit 632a2b3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
53 changes: 52 additions & 1 deletion app/Livewire/OrderDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
28 changes: 27 additions & 1 deletion resources/views/livewire/order-delivery.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
<div>
{{-- Knowing others is intelligence; knowing yourself is true wisdom. --}}
<form wire:submit.prevent="saveDelivery">
<div>
<label for="status">Status</label>
<input type="text" id="status" wire:model="status">
@error('status') <span class="error">{{ $message }}</span> @enderror
</div>

<div>
<label for="description">Description</label>
<textarea id="description" wire:model="description"></textarea>
@error('description') <span class="error">{{ $message }}</span> @enderror
</div>

<div>
<label for="attachments">Attachments</label>
<input type="file" id="attachments" wire:model="attachments" multiple>
@error('attachments.*') <span class="error">{{ $message }}</span> @enderror
</div>

<button type="submit">Create Delivery</button>
</form>

@if (session()->has('success'))
<div>
{{ session('success') }}
</div>
@endif
</div>

0 comments on commit 632a2b3

Please sign in to comment.