/ app / Http / Services / TaxonomyStageService.php
TaxonomyStageService.php
 1  <?php
 2  
 3  namespace App\Http\Services;
 4  
 5  use App\Models\TaxonomyStage;
 6  
 7  class TaxonomyStageService
 8  {
 9      public static function search(int $taxonomyId, string $searchString, string $sortField, string $sortDirection, array|null $filters)
10      {
11          $taxonomyStageQuery = TaxonomyStage::where('taxonomy_id', $taxonomyId)
12              ->where('name', 'like', '%' . $searchString . '%');
13  
14          if ($filters && $filters['withTrashed']) {
15              $taxonomyStageQuery->withTrashed();
16          }
17  
18          if ($filters && $filters['created_at_since']) {
19              $taxonomyStageQuery->where('created_at', '>=', $filters['created_at_since']);
20          }
21  
22          if ($filters && $filters['created_at_to']) {
23              $taxonomyStageQuery->where('created_at', '<=', $filters['created_at_to']);
24          }
25  
26          return $sortField && $sortDirection
27              ? $taxonomyStageQuery->orderBy($sortField, $sortDirection)
28              : $taxonomyStageQuery;
29      }
30  
31      public static function create(array $data)
32      {
33          return TaxonomyStage::create($data);
34      }
35  
36      public static function update(int $id, array $data)
37      {
38          $taxonomyStage = TaxonomyStage::findOrFail($id);
39  
40          return $taxonomyStage->update($data);
41      }
42  
43      public static function delete(int $id)
44      {
45          return TaxonomyStage::findOrFail($id)->delete();
46      }
47  
48      public static function restore(int $id)
49      {
50          return TaxonomyStage::withTrashed()->findOrFail($id)->restore();
51      }
52  }