Files
eatvio-infra/app/Livewire/Diary/ConsultantCommentsEdit.php
2024-01-15 01:05:10 +03:00

76 lines
1.7 KiB
PHP

<?php
namespace App\Livewire\Diary;
use App\Models\ConsultantDiaryComment;
use App\Models\Diary;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Collection;
use Illuminate\View\View;
use Livewire\Component;
class ConsultantCommentsEdit extends Component
{
public int $daysCount;
public Diary $diary;
public array|Collection $curMeals = [];
public array $typeNames;
public array $comments = [];
public string $retireComment = '';
public string $breakfastComment = '';
public string $dinnerComment = '';
public string $supperComment = '';
public string $lunchComment = '';
public string $snacksComment = '';
public string $totalComment = '';
public function mount(): void
{
$this->comments = (new ConsultantDiaryComment())->where(
'user_id',
Auth::id()
)->where(
'diary_id',
$this->diary->id
)->pluck('comment', 'comment_type')->toArray();
}
public function render(): View
{
$commentTypes = ['breakfast', 'dinner', 'supper', 'lunch', 'snacks', 'wakeup_time', 'total'];
return view(
'livewire.diary.consultant-comments-edit',
[
'commentTypes' => $commentTypes,
]
);
}
public function save(): void
{
foreach ($this->comments as $key => $comment) {
$updateComment = ConsultantDiaryComment::updateOrCreate(
[
'user_id' => Auth::id(),
'diary_id' => $this->diary->id,
'comment_type' => $key,
],
['comment' => $comment]
);
$updateComment->save();
}
}
}