File: //home/armgroup/libs/app/Http/Livewire/Account/Ticket/Edit.php
<?php
namespace App\Http\Livewire\Account\Ticket;
use App\Enums\TicketStatus;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Ticket;
use App\Models\TicketAttachment;
use App\Models\TicketMessage;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithFileUploads;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General, WithFileUploads;
public $ticketMessage;
public $items = [];
public $ticket;
public function render()
{
return view('livewire.account.ticket.edit');
}
public function addItem()
{
if (count($this->items) < 5) {
array_push($this->items, [
'name' => null,
'path' => null,
]);
}
}
public function removeItem($index)
{
unset($this->items[$index]);
}
protected function rules()
{
return [
'ticketMessage' => 'required|string|min:3|max:60000',
'items' => 'nullable|array|max:5',
'items.*.name' => 'sometimes|required|string|max:250',
'items.*.path' => 'required|sometimes|mimes:doc,pdf,docx,zip,jpg,png,jpeg|max:20483',
];
}
public function update()
{
$this->validate();
$status = $this->ticket->status->is(TicketStatus::OPEN) ? TicketStatus::OPEN : TicketStatus::CUSTOMER_REPLY;
DB::transaction(function () use ($status) {
Ticket::where('id', $this->ticket->id)->update(['status' => $status]);
$message = TicketMessage::create([
'user_id' => auth()->id(),
'ticket_id' => $this->ticket->id,
'body' => $this->ticketMessage,
]);
if (count($this->items)) {
$folder = auth()->id().'/'.date('Y/m');
foreach ($this->items as $item) {
if ($name = $item['path']->store('attachments/'.$folder)) {
$attachments[] = [
'message_id' => $message->id,
'title' => $item['name'],
'mime' => $item['path']->extension(),
'path' => $name,
];
}
}
TicketAttachment::insert($attachments);
}
});
$this->doneMessage('Updated Successfully');
return redirect()->route('account.tickets.edit', $this->ticket->slug);
}
}