Big Integers
- Enabling Big Integers
- How Values Are Transported
- Objects and Classes
- Submitting Big Integers
- Testing
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 in your config/inertia.php file (or through the INERTIA_PRESERVE_BIG_INTEGERS environment variable)via HandleInertiaRequests::configure.
1'preserve_big_integers' => true,1'preserve_big_integers' => true,
1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.preserve_big_integers(true)6 }7}1use laravel::inertia;23impl inertia::Middleware for HandleInertiaRequests {4 fn configure(&self, config: inertia::Config) -> inertia::Config {5 config.preserve_big_integers(true)6 }7}
Alternatively, you may enable it for a single response using preserveBigIntegers()preserve_big_integers(true). This is useful when only a few pages of your application deal with large integers.
1return Inertia::render('Orders/Show', [2 'order' => $order,3])->preserveBigIntegers();1return Inertia::render('Orders/Show', [2 'order' => $order,3])->preserveBigIntegers();
1inertia::render("Orders/Show", props! { order })2 .preserve_big_integers(true)1inertia::render("Orders/Show", props! { order })2 .preserve_big_integers(true)
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);
1inertia::render("Reports/Index", props! { reports })2 .preserve_big_integers(false)1inertia::render("Reports/Index", props! { reports })2 .preserve_big_integers(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.
1return Inertia::render('Users/Show', [2 'id' => 900719925474099988,3]);45// The response carries {"$bigint": "900719925474099988"}1return Inertia::render('Users/Show', [2 'id' => 900719925474099988,3]);45// The response carries {"$bigint": "900719925474099988"}
1inertia::render("Users/Show", props! {2 id => 900719925474099988_i64,3})45// The response carries {"$bigint": "900719925474099988"}1inertia::render("Users/Show", props! {2 id => 900719925474099988_i64,3})45// The response carries {"$bigint": "900719925474099988"}
1<script setup>2defineProps({ id: BigInt });3</script>45<template>6 <p>User {{ id }}</p>7 <!-- User 900719925474099988 -->8</template>1<script setup>2defineProps({ id: BigInt });3</script>45<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 9007199254740999884}1export default function Show({ id }) {2 return <p>User {String(id)}</p>;3 // User 9007199254740999884}
1<script>2 let { id } = $props();3</script>45<p>User {id}</p>6<!-- User 900719925474099988 -->1<script>2 let { id } = $props();3</script>45<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
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.
Any prop value that implements serde::Serialize—including models, structs, maps, and vectors—is inspected for 64-bit integers outside JavaScript's safe range.
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 });
1public function store(Request $request)2{3 $request->validate(['id' => ['required', 'integer']]);45 $request->integer('id'); // 9007199254740999886}1public function store(Request $request)2{3 $request->validate(['id' => ['required', 'integer']]);45 $request->integer('id'); // 9007199254740999886}
1use laravel::prelude::*;23#[laravel::input]4#[validate(authenticated)]5pub struct StoreOrder {6 #[validate(required)]7 pub id: i64,8}910pub async fn store(Valid(input): Valid<StoreOrder>) -> laravel::Result<Redirect> {11 let id: i64 = input.id; // 9007199254740999881213 Ok(Redirect::back())14}1use laravel::prelude::*;23#[laravel::input]4#[validate(authenticated)]5pub struct StoreOrder {6 #[validate(required)]7 pub id: i64,8}910pub async fn store(Valid(input): Valid<StoreOrder>) -> laravel::Result<Redirect> {11 let id: i64 = input.id; // 9007199254740999881213 Ok(Redirect::back())14}
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.
1$response->assertInertia(fn (Assert $page) => $page2 ->where('order.id', 900719925474099988)3);45$response->inertiaProps('order.id'); // 9007199254740999881$response->assertInertia(fn (Assert $page) => $page2 ->where('order.id', 900719925474099988)3);45$response->inertiaProps('order.id'); // 900719925474099988
1let page = app.get("/orders/1").await.assert_inertia();23page.equals("order.id", 900719925474099988_i64);1let page = app.get("/orders/1").await.assert_inertia();23page.equals("order.id", 900719925474099988_i64);