Created
May 1, 2026 23:21
-
-
Save codigoconjuan/f39c24d18c762ee4a062389eee4a1633 to your computer and use it in GitHub Desktop.
SearchExpense Tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function handle(Request $request): Stringable|string | |
| { | |
| $query = Expense::where('budget_id', $this->budgetId); | |
| if ($request['name'] ?? null) { | |
| $query->where('name', 'ilike', '%' . $request['name'] . '%'); | |
| } | |
| if ($request['category'] ?? null) { | |
| $query->where('category', 'ilike', '%' . $request['category'] . '%'); | |
| } | |
| $expenses = $query->get(['name', 'amount', 'category', 'created_at']); | |
| if ($expenses->isEmpty()) { | |
| return 'No se encontraron gastos con esos criterios.'; | |
| } | |
| $total = $expenses->sum('amount'); | |
| return "Gastos encontrados ({$expenses->count()}):\n" . | |
| $expenses->map(function ($e) { | |
| $cat = $e->category ? $e->category->label() : 'Sin categoría'; | |
| return "- {$e->name}: \${$e->amount} ({$cat})"; | |
| })->implode("\n") . | |
| "\n\nTotal: \${$total}"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment