Step 7 — Create tasks

Northwind staff need to add tasks, not only list them.

What you will learn

  • Add createAction with table('tasks')->save()
  • Confirm inserts with a second task.list call
Before you start

Add createAction

In services/TaskService.php:

use Pionia\Exceptions\ValidationException;

protected function createAction(Arrayable $data): ApiResponse
{
    $title = $data->getString('title');
    if ($title === null || trim($title) === '') {
        throw new ValidationException('title is required');
    }

    $task = table('tasks')->save([
        'title' => trim($title),
        'status' => $data->getString('status', 'open'),
        'assignee' => $data->getString('assignee', 'alex@northwind.studio'),
    ]);

    return response(0, 'Task created', ['task' => $task]);
}

Create a row

curl -s -X POST http://127.0.0.1:8000/api/v1/ \
  -H "Content-Type: application/json" \
  -d '{"service":"task","action":"create","title":"Ship DeskFlow tutorial"}'

Run task.list again — your new row appears.

Milestone: You now have read + write against SQLite. Step 8 replaces inline checks with declarative validation.

Common mistakes

  • HTTP 500 instead of 422 when title missing — ensure you throw ValidationException, not plain Exception.