Generates a Query class for read-only operations. Use when implementing complex data retrieval, reports, or optimized fetching (e.g., "Create a query to list students with GPA").
This skill generates a Query class for handling read logic, keeping Controllers thin and separating "Writes" (Actions) from "Reads" (Queries).
Identify Parameters:
Academic, Identity).Student, MonthlyRevenue).List, Get, Find, Report.{Verb}{Entity}[Context]Query (e.g., ListStudentsQuery, GetStudentGpaQuery).Determine Path:
app/Modules/{Module}/Queries/{ClassName}.phpGenerate Code:
App\Modules\{Module}\Queriespublic function handle(...)create, update, delete, or transaction calls.Collection, Paginator, DTO).<?php ș
namespace App\Modules\Academic\Queries;
use App\Modules\Academic\Models\Student;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
class ListStudentsQuery
{
/**
* Get a paginated list of students with filters.
*
* @param int $perPage
* @return LengthAwarePaginator
*/
public function handle(int $perPage = 15): LengthAwarePaginator
{
return QueryBuilder::for(Student::class)
->allowedFilters([
AllowedFilter::partial('name'),
AllowedFilter::exact('status'),
AllowedFilter::exact('campus_id'),
])
->allowedSorts(['created_at', 'name'])
->defaultSort('-created_at')
->paginate($perPage)
->withQueryString();
}
}
select(), with() (eager loading) to prevent N+1 issues.currentCampus()).