Skip to content

Prop types

Prop types change when and how a prop is sent. They are created through the Inertia facade and live in the Crenspire\Yii2Inertia\Props namespace. All of them can be nested at any depth in the props.

FactoryClassInitial visitPartial reloadGuide
Inertia::optional()OptionalPropnot sentwhen requestedPartial reloads
Inertia::defer()DeferPropannounced, loaded right afterwhen requestedDeferred props
Inertia::always()AlwaysPropsentalways sentPartial reloads
Inertia::merge()MergePropsentsent and mergedMerging props
Inertia::deepMerge()MergePropsentsent and deep mergedMerging props
Inertia::once()OncePropsent unless rememberedwhen requestedOnce props
Inertia::scroll()ScrollPropsent with pagination metadatasent and mergedInfinite scroll

Modifiers

Merging

Available on MergeProp, DeferProp and ScrollProp.

MethodDescription
merge()Merge the value instead of replacing it
deepMerge()Merge recursively
append(bool|string|array $path = true, ?string $matchOn = null)Append at the root or at nested paths
prepend(bool|string|array $path = true, ?string $matchOn = null)Prepend at the root or at nested paths
matchOn(string|array $keys)Keys identifying items to update in place

Once

Available on OnceProp, OptionalProp, DeferProp and MergeProp.

MethodDescription
once(bool $value = true, ?string $as = null, DateTimeInterface|DateInterval|int|null $until = null)Resolve once and remember on the client
as(BackedEnum|UnitEnum|string $key)Remember under a custom key
until(DateTimeInterface|DateInterval|int $delay)Expire after a date, interval or number of seconds
fresh(bool $value = true)Send the value even if the client has it

Deferring

Available on DeferProp and ScrollProp.

MethodDescription
defer(?string $group = null)Load in a separate request, in the given group

DeferProp also accepts rescue in its constructor (Inertia::defer($callback, $group, rescue: true)).

Scroll metadata

ScrollMetadata describes the pagination state of a scroll prop:

php
new ScrollMetadata(string $pageName, int|string|null $previousPage = null, int|string|null $nextPage = null, int|string|null $currentPage = null)

ScrollMetadata::fromPagination(\yii\data\Pagination $pagination): ScrollMetadata
ScrollMetadata::fromDataProvider(\yii\data\DataProviderInterface $dataProvider): ScrollMetadata

Implement ProvidesScrollMetadata for custom pagination objects.

Property providers

InterfaceMethodDescription
ProvidesInertiaPropertiestoInertiaProperties(RenderContext $context): iterableAdds several props; used without a key
ProvidesInertiaPropertytoInertiaProperty(PropertyContext $prop): mixedComputes a single prop value

RenderContext has the component name and the request. PropertyContext has the prop key (dot-notation path), its sibling props and the request.

Value resolution

Every prop value is resolved in this order before it is encoded:

  1. Invokable objects and closures are called.
  2. ProvidesInertiaProperty objects are asked for their value.
  3. yii\base\Arrayable objects are converted with toArray().
  4. JsonSerializable objects are serialized.
  5. Arrays are resolved recursively.

Released under the MIT License.