diff --git a/app/Http/Controllers/CRUDController.php b/app/Http/Controllers/CRUDController.php index 55cf2a03..6c8d0ddf 100644 --- a/app/Http/Controllers/CRUDController.php +++ b/app/Http/Controllers/CRUDController.php @@ -30,13 +30,6 @@ abstract class CRUDController extends Controller */ protected array $rules = []; - /** - * The columns to search in. - * - * @var array - */ - protected array $search = []; - /** * The validation rules for the store method. * @@ -76,29 +69,13 @@ protected function with(): array public function index(Request $request) { - $sort_by = $request->query('sort_by', 'id'); - $sort_dir = $request->query('sort_dir', 'asc'); - $query = $this->model::orderBy($sort_by, $sort_dir); - - $filter_by = $request->query('filter_by'); - - if ($filter_by) { - foreach ($filter_by as $column => $values) { - $query->whereIn($column, $values); - } - } + $isSearchable = in_array(\Laravel\Scout\Searchable::class, class_uses($this->model)); $search = $request->query('query'); - - if ($search) { - $search = explode(' ', $search); - foreach ($search as $searchTerm) { - $query->where(function ($query) use ($searchTerm) { - foreach ($this->search as $column) { - $query->orWhere($column, 'ILIKE', "%{$searchTerm}%"); - } - }); - } + if ($isSearchable && $search !== null) { + $query = $this->model::search($search); + } else { + $query = $this->model::orderBy('id'); } $filteredQuery = collect($request->query()) @@ -107,12 +84,10 @@ public function index(Request $request) $with = $this->with(); - return Inertia::render("CRUD/$this->view/Index", [ + return Inertia::render("CRUD/{$this->view}/Index", [ 'items' => $items, 'with' => $with, - 'sortBy' => $sort_by, - 'sortDir' => $sort_dir, - 'filterBy' => $filter_by, + 'isSearchable' => $isSearchable, ]); } diff --git a/app/Models/Admin.php b/app/Models/Admin.php index 7e2fdc43..95759191 100644 --- a/app/Models/Admin.php +++ b/app/Models/Admin.php @@ -23,4 +23,9 @@ public function user(): BelongsTo { return $this->belongsTo(User::class); } + + public function toSearchableArray(): array + { + return []; + } } diff --git a/app/Models/Company.php b/app/Models/Company.php index 43baac10..d3e8d82c 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -69,4 +69,12 @@ public function participants(): HasManyThrough ] )->distinct(); } + + public function toSearchableArray(): array + { + return [ + 'description' => $this->description, + 'social_media' => $this->socialMedia->toSearchableArray(), + ]; + } } diff --git a/app/Models/Participant.php b/app/Models/Participant.php index f35a39f0..d7609151 100644 --- a/app/Models/Participant.php +++ b/app/Models/Participant.php @@ -59,4 +59,11 @@ public function competitionTeams(): BelongsToMany { return $this->belongsToMany(CompetitionTeam::class); } + + public function toSearchableArray(): array + { + return [ + 'social_media' => $this->socialMedia->toSearchableArray(), + ]; + } } diff --git a/app/Models/Speaker.php b/app/Models/Speaker.php index 9855a1ae..9d028472 100644 --- a/app/Models/Speaker.php +++ b/app/Models/Speaker.php @@ -34,4 +34,14 @@ public function user(): BelongsTo { return $this->belongsTo(User::class); } + + public function toSearchableArray(): array + { + return [ + 'title' => $this->title, + 'description' => $this->description, + 'organization' => $this->organization, + 'social_media' => $this->socialMedia->toSearchableArray(), + ]; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 6d9a8a15..21b96f97 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,6 +10,7 @@ use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Sanctum\HasApiTokens; +use Laravel\Scout\Searchable; class User extends Authenticatable { @@ -17,6 +18,7 @@ class User extends Authenticatable use HasFactory; use HasProfilePhoto; use Notifiable; + use Searchable; use TwoFactorAuthenticatable; /** @@ -95,4 +97,15 @@ public function isSpeaker(): bool { return $this->usertype_type === Speaker::class; } + + public function toSearchableArray() + { + return [ + 'id' => (int) $this->id, + 'name' => $this->name, + 'email' => $this->email, + 'type' => call_user_func('end', explode('\\', $this->usertype_type)), + 'user' => $this->usertype->toSearchableArray(), + ]; + } } diff --git a/config/scout.php b/config/scout.php index ddea1310..dde343c9 100644 --- a/config/scout.php +++ b/config/scout.php @@ -133,16 +133,6 @@ 'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'), 'key' => env('MEILISEARCH_KEY'), 'index-settings' => [ - \App\Models\User::class => [ - 'filterableAttributes' => [ - 'type', - ], - 'sortableAttributes' => [ - 'id', - 'name', - 'email', - ], - ], ], ], diff --git a/resources/js/Components/CRUD/Header.vue b/resources/js/Components/CRUD/Header.vue index f1e0a507..3f007528 100644 --- a/resources/js/Components/CRUD/Header.vue +++ b/resources/js/Components/CRUD/Header.vue @@ -1,126 +1,5 @@ - - diff --git a/resources/js/Layouts/CRUDLayout.vue b/resources/js/Layouts/CRUDLayout.vue index 9527b957..17698f54 100644 --- a/resources/js/Layouts/CRUDLayout.vue +++ b/resources/js/Layouts/CRUDLayout.vue @@ -13,6 +13,7 @@ defineProps<{ items: Paginated; title: string; name: string; + isSearchable?: boolean; }>(); const query = useSearch("query", ["items"]); @@ -28,7 +29,12 @@ const query = useSearch("query", ["items"]); >

- + Novo diff --git a/resources/js/Pages/CRUD/User/Index.vue b/resources/js/Pages/CRUD/User/Index.vue index a712976e..24c6a254 100644 --- a/resources/js/Pages/CRUD/User/Index.vue +++ b/resources/js/Pages/CRUD/User/Index.vue @@ -9,6 +9,7 @@ import Header from "@/Components/CRUD/Header.vue"; interface Props { items: Paginated; + isSearchable?: boolean; } defineProps(); @@ -22,7 +23,12 @@ const usertypeMap = {