Search by

nickwelsh / zero-for-laravel

nickwelsh

Controller-first Laravel queries and mutations for Rocicorp Zero.

Package info

github.com/nickwelsh/zero-for-laravel

pkg:composer/nickwelsh/zero-for-laravel

Statistics

Installs: 54

Dependents: 0

Suggesters: 0

Stars: 0

v0.15.0 2026-11-23 03:55 UTC

This package is auto-updated.

Last update: 2026-11-27 21:10:47 UTC


README

Controller-first live queries and mutations for Rocicorp Zero 1.11.4.

Use v0.15.0 with the matching npm packages. Generate and deploy PHP registrations and TypeScript contracts together. PHP 8.3+, Laravel 12/13, and PostgreSQL 15+ with logical replication are required targets. Automatic live page props require Inertia Laravel 3 and the optional JavaScript Inertia adapter.

Controllers build Model::zeroQuery() live props with Eloquent methods and explicitly opt actions into #[ZeroMutation]. Ranger/Surveyor-backed generation emits coordinated PHP registrations, native ZQL/mutators, validation schemas, result codecs, and named controller modules. Laravel retains real binding, middleware, FormRequests, Gates, and Eloquent. Zero retains replication, optimistic replay, ordering, and durable mutation results.

An unconditional $this->authorize('view', $todo) and a returned bound $todo compile to a guarded live query. A generated create can return a checked RedirectResponse to open that page using the locally prepared ID. See the bound model example and the form migration.

For component-owned reads, put public methods returning ZeroForLaravel\Query\ZeroQuery in ordinary application classes such as App\Zero\Queries\TaskQueries. zero:generate discovers them through Composer without route registration and exports callable queries with .live() bindings. Use zero.run(search(input)), native useQuery, or useData(search.live(input)). Standalone reads validate and authorize their own FormRequest through the existing Zero endpoint; they do not inherit page middleware or route model binding.

Run php artisan zero:setup to opt into Inertia live props and Wayfinder dev-next page types. Laravel discovers the providers; ZERO_INERTIA and ZERO_WAYFINDER enable them independently. Run Zero generation before Wayfinder generation.

php artisan vendor:publish --tag=zero-config
php artisan migrate
php artisan zero:generate --react
php artisan zero:sync-publication --dry-run

Migrations install the private binding between each mutation client group and its authenticated Zero identity. After upgrading, reset clients whose existing progress predates that binding so they reconnect with a new client group; the server refuses to guess ownership of legacy bookkeeping.

Review and apply publication changes to the application's configured PostgreSQL database. Deploy generated PHP and TypeScript together; use zero:generate --check in CI. Keep the cache API key server-side and independently authenticate end users.

Optional Effect schemas

Zod remains the default generated validator. Applications using Effect 4 can set generation.schema to ZeroForLaravel\Generation\EffectSchemaDriver::class in config/zero.php, install effect in their JavaScript application, and rerun php artisan zero:generate. The Effect driver is tested with Effect 4.0.2; it does not require Formbraid or a Formbraid upgrade.

Generated mutation actions expose native Effect schemas as inputSchema and serverResultSchema. They remain usable with Effect composition and Formbraid's schema option, while retaining the synchronous .parse() API used by Zero:

import { Schema } from 'effect';
import { store } from '@/zero/App/Http/Controllers/TodoController';

const input = store.inputSchema.parse({ title: 'New task' });
const batch = Schema.Array(store.inputSchema);

Generated query validators also implement Standard Schema for native Zero. Both compatibility adapters reject unknown object keys and preserve nested field errors. When decoding a composed schema through Effect directly, pass { onExcessProperty: 'error' } to its decoder to retain that strictness; Effect's default otherwise strips unknown keys. Generated validation is synchronous and requires no Effect services. Add asynchronous or service-dependent form validation outside Zero's optimistic replay.

The driver changes only generated TypeScript validators. PHP registrations, wire encodings, and mutation fingerprints stay unchanged. Effect imports occur only in opted-in generated files, never in the core or table runtime packages.

See the monorepo guide and shared todos example. Development and issues belong in the source monorepo, not the distribution mirror.