/ app / Http / Livewire / TOES / Category / Create.php
Create.php
 1  <?php
 2  
 3  namespace App\Http\Livewire\TOES\Category;
 4  
 5  use App\Http\Services\ToeCategoryService;
 6  use Exception;
 7  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 8  use Livewire\Component;
 9  use WireUi\Traits\Actions;
10  
11  class Create extends Component
12  {
13      use AuthorizesRequests, Actions;
14  
15      public array $category = [
16          'name' => '',
17      ];
18  
19      protected $rules = [
20          'category.name' => 'required|min:2',
21          'category.code' => 'nullable|integer',
22      ];
23  
24      public function mount()
25      {
26          $this->authorize('can_write_toes');
27      }
28  
29      public function updated($propertyName)
30      {
31          $this->validateOnly($propertyName);
32      }
33  
34      public function create()
35      {
36          $this->authorize('can_write_toes');
37          $this->validate();
38  
39          try {
40              ToeCategoryService::create($this->category);
41  
42              // UI Trick for delaying spinner
43              if (config('app.env') !== 'testing') {
44      sleep(1);
45  }
46  
47              $this->emitUp('closePanel');
48              $this->emitTo('t-o-e-s.category.index', 'refresh');
49  
50              $this->notification()->success(
51                  __('toes.category.notifications.create.success.title'),
52                  __('toes.category.notifications.create.success.message')
53              );
54          } catch (Exception $e) {
55              $this->notification()->error(
56                  'Error',
57                  $e->getMessage()
58              );
59          }
60      }
61  
62      public function render()
63      {
64          return view('livewire.toes.category.create');
65      }
66  }