122 lines
No EOL
2.9 KiB
PHP
122 lines
No EOL
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Dishes;
|
|
|
|
use App\Models\Dish;
|
|
use App\Models\User;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
class DishesList extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
public $showCreateModal = false;
|
|
public $showEditModal = false;
|
|
public $showDeleteModal = false;
|
|
|
|
public $editingDish = null;
|
|
public $deletingDish = null;
|
|
|
|
// Form fields
|
|
public $name = '';
|
|
public $selectedUsers = [];
|
|
|
|
protected $rules = [
|
|
'name' => 'required|string|max:255',
|
|
'selectedUsers' => 'array',
|
|
];
|
|
|
|
public function render()
|
|
{
|
|
$dishes = Dish::with('users')
|
|
->orderBy('name')
|
|
->paginate(10);
|
|
|
|
$users = User::where('planner_id', auth()->user()->planner_id)
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return view('livewire.dishes.dishes-list', [
|
|
'dishes' => $dishes,
|
|
'users' => $users
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->reset(['name', 'selectedUsers']);
|
|
$this->resetValidation();
|
|
$this->showCreateModal = true;
|
|
}
|
|
|
|
public function store()
|
|
{
|
|
$this->validate();
|
|
|
|
$dish = Dish::create([
|
|
'name' => $this->name,
|
|
'planner_id' => auth()->user()->planner_id,
|
|
]);
|
|
|
|
// Attach selected users
|
|
if (!empty($this->selectedUsers)) {
|
|
$dish->users()->attach($this->selectedUsers);
|
|
}
|
|
|
|
$this->showCreateModal = false;
|
|
$this->reset(['name', 'selectedUsers']);
|
|
|
|
session()->flash('success', 'Dish created successfully.');
|
|
}
|
|
|
|
public function edit(Dish $dish)
|
|
{
|
|
$this->editingDish = $dish;
|
|
$this->name = $dish->name;
|
|
$this->selectedUsers = $dish->users->pluck('id')->toArray();
|
|
$this->resetValidation();
|
|
$this->showEditModal = true;
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->validate();
|
|
|
|
$this->editingDish->update([
|
|
'name' => $this->name,
|
|
]);
|
|
|
|
// Sync users
|
|
$this->editingDish->users()->sync($this->selectedUsers);
|
|
|
|
$this->showEditModal = false;
|
|
$this->reset(['name', 'selectedUsers', 'editingDish']);
|
|
|
|
session()->flash('success', 'Dish updated successfully.');
|
|
}
|
|
|
|
public function confirmDelete(Dish $dish)
|
|
{
|
|
$this->deletingDish = $dish;
|
|
$this->showDeleteModal = true;
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
$this->deletingDish->users()->detach();
|
|
$this->deletingDish->delete();
|
|
$this->showDeleteModal = false;
|
|
$this->deletingDish = null;
|
|
|
|
session()->flash('success', 'Dish deleted successfully.');
|
|
}
|
|
|
|
public function cancel()
|
|
{
|
|
$this->showCreateModal = false;
|
|
$this->showEditModal = false;
|
|
$this->showDeleteModal = false;
|
|
$this->reset(['name', 'selectedUsers', 'editingDish', 'deletingDish']);
|
|
}
|
|
} |