<!-- Source: @site/projects/polyslug-for-laravel/docs/features/computed-addresses.md -->
<!-- Canonical: https://docs.pushery.com/polyslug-for-laravel/features/computed-addresses/ -->

# Addresses for a read model

> A token from a column of the record and a slug computed from its source, for records that are rebuilt under new keys and keep nothing in the package's tables.

A read model is rebuilt whenever its source changes: a catalog projected from another system,
emptied and written again, its rows under new primary keys each time. Its pages still need
addresses that last. `identity` takes the token from a column of the record instead of the
encoded primary key, and `stored: false` computes the slug from the source on every call
instead of writing it:

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Polyslug\Attributes\Polyslug;
use Polyslug\Concerns\HasPolyslug;
use Polyslug\Contracts\Sluggable;

#[Polyslug(source: 'name', unique: false, stored: false, identity: 'address_token')]
final class CatalogProduct extends Model implements Sluggable
{
    use HasPolyslug;
}

$product = CatalogProduct::query()->firstOrFail();
$product->getRouteKey(); // linen-shirt_01j9x7k2m4
```

Nothing of such a record is written to `polyslug_slugs` or `polyslug_tokens`, so the package's
tables never hold rows of a tenant whose own tables carry a tenant column, and a record written
again under a new key answers at the same address.

A [short link](https://docs.pushery.com/polyslug-for-laravel/features/short-links.md) is the one exception. `shortLink()` writes a row to
`polyslug_short_links` that names the record by its primary key, so that table does hold a row
of the tenant, and once the record is written again under a new key, the old link answers 404
and the record gets a new one. Leave short links out for such a model, or give it a primary key
that survives the rewrite.

## The token from a column

`identity` names the column whose value follows the delimiter, and a URL resolves by comparing
its token with that column, through the [resolution gate](https://docs.pushery.com/polyslug-for-laravel/features/access-control.md). The value takes
letters, digits and hyphens; a value with anything else throws `InvalidArgumentException` when
its URL is built. The column wants a unique index of its own.

A stable id from the other system makes a good token, and the end of it makes a short one: the
last ten characters of a ULID are random. `Polyslug\Support\TailTokens` takes them and settles
two ids that end alike, the same way whatever order they arrive in:

```php
use Polyslug\Support\TailTokens;

TailTokens::of('01J9X7K2M4ABCDEFGHJK', 10); // abcdefghjk

TailTokens::apart(['a' => '01HZXYABCDEFGHJK0123', 'b' => '01J9X7K2M4ABCDEFGHJK0123'], 10);
// ['a' => 'efghjk0123', 'b' => '01j9x7k2m4abcdefghjk0123']
```

The id that sorts first, which for a ULID or a UUIDv7 is the older one, keeps the short token,
and every other one answers under its whole id. Store the result in the identity column when the
records are written, and hand `apart()` every id that can end like them, the stored ones
included.

## An address the column no longer holds

A token from `apart()` moves when the ids that end like it change. The older of two ids leaves,
and the younger takes the short token; a URL written with the whole id, where the short token
would do, names the record as well. Name the column the token was taken from, and both still
resolve:

```php
#[Polyslug(source: 'name', unique: false, stored: false, identity: 'address_token', identityOf: 'public_id')]
```

A token the identity column does not hold is then read as an id, written the way
`TailTokens::whole()` writes one, and it resolves to the record whose `public_id` that is. The
[canonical middleware](https://docs.pushery.com/polyslug-for-laravel/features/self-healing-routes.md) answers the URL with a 301 to the token of today.
The record is looked up under every token `apart()` can have given it, each end of the id and
the whole id, so the search stays on the identity column and its unique index.

Another system can hang one id on two records for a while, as it moves the id from one to the
other. Hand `apart()` a second id of each, such as that system's own id of the record, and the
later of the two answers under `{id}-{second id}` instead of `{id}-2`. The second id decides
which one comes first, so neither token depends on the order the records arrive in:

```php
TailTokens::apart(
    [1 => '01HZXYABCDEFGHJK0123', 2 => '01J9X7K2M4ABCDEFGHJK0123', 3 => '01J9X7K2M4ABCDEFGHJK0123'],
    10,
    [1 => 'cup', 2 => 'plater', 3 => 'plate'],
);
// [1 => 'efghjk0123', 3 => '01j9x7k2m4abcdefghjk0123', 2 => '01j9x7k2m4abcdefghjk0123-plater']
```

Name the column of the second id as well, `identitySecond: 'origin_id'`, and a URL with the
second id after the whole id resolves to the record both name, also after the other record has
left and the token has moved. A number after the second id, the `-2` of two records whose second
ids are equal too, is not read: it only tells a record apart for as long as the other one is
there. Without `identitySecond`, nothing after the whole id is read.

`polyslugIsGoneAddress()` receives the token as the URL carried it, and `TailTokens::idsIn()`
reads the ids out of it, so a table of withdrawn records can be searched by them.

## The slug computed from the source

With `stored: false` the slug is built from the source each time it is asked for, the way a
stored one would be generated, and kept nowhere. Renaming the record changes its URL at once.
The old URL still resolves by its token, and the [canonical middleware](https://docs.pushery.com/polyslug-for-laravel/features/self-healing-routes.md)
answers it with a 301 to the current slug. There is no history to keep, so `slugHistory()` is
empty and `retireSlug()` retires nothing. Two records may compute the same slug, which is why
`unique: false` is required: the token after it tells them apart.

The source is read from the configured columns, the same text in every locale. A record that
holds its text per language overrides `polyslugComputedSource()`:

```php
public function polyslugComputedSource(string $locale): string
{
    return $this->names[$locale] ?? $this->name;
}
```

A computed slug is announced in the default locale. A record served under more locales names
them through [`ProvidesAddressLocales`](https://docs.pushery.com/polyslug-for-laravel/reference/contracts.md#providesaddresslocales), and
`hreflangLinks()`, `polyslugUrls()` and [`polyslug:sitemap`](https://docs.pushery.com/polyslug-for-laravel/features/sitemaps.md) then list each locale
under the slug of its own text.

A slugless model may set `stored: false` as well: its URL is the token alone, and it writes no
row at all.

## Gone without a record

A read model drops a withdrawn record, so no route binding can find it to answer gone. Override
`polyslugIsGoneAddress()` to name the tokens it withdrew; when a token resolves to nothing and
the method says yes, the binding answers with `polyslug.gone.status` (410 by default) instead of
404:

```php
public function polyslugIsGoneAddress(string $token): bool
{
    return WithdrawnProduct::query()->where('token', $token)->exists();
}
```

The answer comes before the application's own, as a 404 does, so name only tokens whose
withdrawal anyone may learn. For a record that is still stored, `polyslugIsGone()` remains the
place to say it: see [Gone and superseded](https://docs.pushery.com/polyslug-for-laravel/features/gone-and-superseded.md).
