Skip to content

Big Integers

Advanced

Big Integers

v3.8.0+ Laravel Adapter v3.5.0+

Sometimes you may wish to pass integers that exceed JavaScript's safe integer range, such as IDs from a 64-bit database column. JavaScript rounds those values while parsing JSON, so 900719925474099988 reaches your components as 900719925474100000. Big integer support keeps them exact by delivering them as native BigInt values, following the JSON convention documented on MDN.

Enabling Big Integers

Big integer support is disabled by default and is enabled entirely on the server, so there is nothing to configure on the client. You may enable it for every response via HandleInertiaRequests::configureby setting PreserveBigIntegers: true on inertia.Middlewarein your config/inertia.php file (or through the INERTIA_PRESERVE_BIG_INTEGERS environment variable).

1use laravel::inertia;
2 
3impl inertia::Middleware for HandleInertiaRequests {
4 fn configure(&self, config: inertia::Config) -> inertia::Config {
5 config.preserve_big_integers(true)
6 }
7}
1use laravel::inertia;
2 
3impl inertia::Middleware for HandleInertiaRequests {
4 fn configure(&self, config: inertia::Config) -> inertia::Config {
5 config.preserve_big_integers(true)
6 }
7}
1m.Web.Append(inertia.Middleware{
2 PreserveBigIntegers: true,
3})
1m.Web.Append(inertia.Middleware{
2 PreserveBigIntegers: true,
3})
config/inertia.php
1'preserve_big_integers' => true,
1'preserve_big_integers' => true,

Alternatively, you may enable it for a single response using preserve_big_integers(true)inertia.PreserveBigIntegers(r)preserveBigIntegers(). This is useful when only a few pages of your application deal with large integers.

1inertia::render("Orders/Show", props! { order })
2 .preserve_big_integers(true)
1inertia::render("Orders/Show", props! { order })
2 .preserve_big_integers(true)
1inertia.PreserveBigIntegers(r)
2 
3return inertia.Render("Orders/Show", inertia.Props{
4 "order": order,
5})
1inertia.PreserveBigIntegers(r)
2 
3return inertia.Render("Orders/Show", inertia.Props{
4 "order": order,
5})
1return Inertia::render('Orders/Show', [
2 'order' => $order,
3])->preserveBigIntegers();
1return Inertia::render('Orders/Show', [
2 'order' => $order,
3])->preserveBigIntegers();

Passing false opts a single response out, which is useful once the feature is enabled globally.

1inertia::render("Reports/Index", props! { reports })
2 .preserve_big_integers(false)
1inertia::render("Reports/Index", props! { reports })
2 .preserve_big_integers(false)

Passing false opts a single response out, which is useful once the feature is enabled globally.

1return Inertia::render('Reports/Index', $props)->preserveBigIntegers(false);
1return Inertia::render('Reports/Index', $props)->preserveBigIntegers(false);

How Values Are Transported

Integers outside the safe range are wrapped in a marker object before the response is encoded. The client turns that marker back into a BigInt, so your component receives the exact value.

1inertia::render("Users/Show", props! {
2 id => 900719925474099988_i64,
3})
4 
5// The response carries {"$bigint": "900719925474099988"}
1inertia::render("Users/Show", props! {
2 id => 900719925474099988_i64,
3})
4 
5// The response carries {"$bigint": "900719925474099988"}
1return inertia.Render("Users/Show", inertia.Props{
2 "id": int64(900719925474099988),
3})
4 
5// The response carries {"$bigint": "900719925474099988"}
1return inertia.Render("Users/Show", inertia.Props{
2 "id": int64(900719925474099988),
3})
4 
5// The response carries {"$bigint": "900719925474099988"}
1return Inertia::render('Users/Show', [
2 'id' => 900719925474099988,
3]);
4 
5// The response carries {"$bigint": "900719925474099988"}
1return Inertia::render('Users/Show', [
2 'id' => 900719925474099988,
3]);
4 
5// The response carries {"$bigint": "900719925474099988"}
1<script setup>
2defineProps({ id: BigInt });
3</script>
4 
5<template>
6 <p>User {{ id }}</p>
7 <!-- User 900719925474099988 -->
8</template>
1<script setup>
2defineProps({ id: BigInt });
3</script>
4 
5<template>
6 <p>User {{ id }}</p>
7 <!-- User 900719925474099988 -->
8</template>
1export default function Show({ id }) {
2 return <p>User {String(id)}</p>;
3 // User 900719925474099988
4}
1export default function Show({ id }) {
2 return <p>User {String(id)}</p>;
3 // User 900719925474099988
4}
1<script>
2 let { id } = $props();
3</script>
4 
5<p>User {id}</p>
6<!-- User 900719925474099988 -->
1<script>
2 let { id } = $props();
3</script>
4 
5<p>User {id}</p>
6<!-- User 900719925474099988 -->

Integers within the safe range are left as regular numbers, so only the values that need it become a BigInt. The same prop may therefore arrive as a number or a BigInt, depending on its value. Flash data receives the same treatment as props.

Markers are only revived on pages that enable big integer support, so applications that already send their own {"$bigint": "..."} objects continue to receive them as plain objects everywhere else.

Objects and Classes

Any prop value that implements serde::Serialize—including models, structs, maps, and vectors—is inspected for 64-bit integers outside JavaScript's safe range.

Any prop value that encodes to JSON—including models, structs, maps, and slices—is inspected for integers outside JavaScript's safe range.

Arrays, plain objects, Eloquent models, collections, API resources, and values implementing JsonSerializable are all inspected for large integers, as are the public properties of your own classes. Built-in PHP classes such as DateTime, along with classes extending them, are passed through untouched, since they serialize through their own handlers.

Submitting Big Integers

A BigInt submitted through the router, a form, or Precognition is sent as its digits, the same way form data and query strings send numbers. Your controller or handler receives a numeric value, which validation handles as usual.

1router.post("/orders", { id: props.order.id });
1router.post("/orders", { id: props.order.id });
1use laravel::prelude::*;
2 
3#[laravel::input]
4#[validate(authenticated)]
5pub struct StoreOrder {
6 #[validate(required)]
7 pub id: i64,
8}
9 
10pub async fn store(Valid(input): Valid<StoreOrder>) -> laravel::Result<Redirect> {
11 let id: i64 = input.id; // 900719925474099988
12 
13 Ok(Redirect::back())
14}
1use laravel::prelude::*;
2 
3#[laravel::input]
4#[validate(authenticated)]
5pub struct StoreOrder {
6 #[validate(required)]
7 pub id: i64,
8}
9 
10pub async fn store(Valid(input): Valid<StoreOrder>) -> laravel::Result<Redirect> {
11 let id: i64 = input.id; // 900719925474099988
12 
13 Ok(Redirect::back())
14}
1type StoreOrder struct {
2 ID int64 `json:"id"`
3}
4 
5func (f *StoreOrder) Rules() validation.Rules {
6 return validation.For(f,
7 rule.Field(&f.ID, rule.Required),
8 )
9}
10 
11func Store(r *web.Request) *web.Response {
12 form, err := r.Validate[StoreOrder]()
13 if err != nil {
14 return web.Fail(err)
15 }
16 
17 id := form.ID // 900719925474099988
18 
19 return web.Back()
20}
1type StoreOrder struct {
2 ID int64 `json:"id"`
3}
4 
5func (f *StoreOrder) Rules() validation.Rules {
6 return validation.For(f,
7 rule.Field(&f.ID, rule.Required),
8 )
9}
10 
11func Store(r *web.Request) *web.Response {
12 form, err := r.Validate[StoreOrder]()
13 if err != nil {
14 return web.Fail(err)
15 }
16 
17 id := form.ID // 900719925474099988
18 
19 return web.Back()
20}
1public function store(Request $request)
2{
3 $request->validate(['id' => ['required', 'integer']]);
4 
5 $request->integer('id'); // 900719925474099988
6}
1public function store(Request $request)
2{
3 $request->validate(['id' => ['required', 'integer']]);
4 
5 $request->integer('id'); // 900719925474099988
6}

The useHttp hook does not handle BigInt values, since it is commonly used to call third-party APIs. Convert those values to a string before sending them.

Testing

Inertia's testing helpers turn markers back into integers, so you may assert against the same values you passed to the response.In tests, big integers are encoded as {"$bigint": "..."} marker objects in the page payload, so you may assert against the marker with AssertProp.Inertia's testing helpers turn markers back into integers, so you may assert against the same values you passed to the response.

1let page = app.get("/orders/1").await.assert_inertia();
2 
3page.equals("order.id", 900719925474099988_i64);
1let page = app.get("/orders/1").await.assert_inertia();
2 
3page.equals("order.id", 900719925474099988_i64);
1client.Get("/orders/1").
2 AssertInertia("Orders/Show").
3 AssertProp("order.id", map[string]any{"$bigint": "900719925474099988"})
1client.Get("/orders/1").
2 AssertInertia("Orders/Show").
3 AssertProp("order.id", map[string]any{"$bigint": "900719925474099988"})
1$response->assertInertia(fn (Assert $page) => $page
2 ->where('order.id', 900719925474099988)
3);
4 
5$response->inertiaProps('order.id'); // 900719925474099988
1$response->assertInertia(fn (Assert $page) => $page
2 ->where('order.id', 900719925474099988)
3);
4 
5$response->inertiaProps('order.id'); // 900719925474099988