Step 6 — List tasks from the database

Wire listAction to the tasks table you seeded in Step 5.

What you will learn

  • Read rows with global helper table()
  • Return ORM results inside a Moonlight envelope
Before you start
  • Step 5database.sqlite3 exists

Update TaskService

services/TaskService.php:

protected function listAction(Arrayable $data): ApiResponse
{
    $tasks = table('tasks')->all();
    return response(0, 'OK', ['tasks' => $tasks]);
}

Verify

Try it yourself
curl -s -X POST http://127.0.0.1:8000/api/v1/ \
  -H "Content-Type: application/json" \
  -d '{"service":"task","action":"list"}'

You should see three tasks from schema.sql (including Jamie’s brief).

More query patterns: Making queries.

Common mistakes

  • Still seeing only one hardcoded task — save the file; confirm you removed the PHP array.
  • Empty list after re-init — re-run php bin/init-db.php if you deleted the SQLite file.