CINXE.COM
Helpers - Laravel 11.x - The PHP Framework For Web Artisans
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Helpers - Laravel 11.x - The PHP Framework For Web Artisans</title> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> <link rel="canonical" href="https://laravel.com/docs/11.x/helpers"> <!-- Primary Meta Tags --> <meta name="title" content="Laravel - The PHP Framework For Web Artisans"> <meta name="description" content="Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things."> <!-- Open Graph / Facebook --> <meta property="og:type" content="website"> <meta property="og:url" content="https://laravel.com/"> <meta property="og:title" content="Laravel - The PHP Framework For Web Artisans"> <meta property="og:description" content="Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things."> <meta property="og:image" content="https://laravel.com/img/og-image.jpg"> <!-- Twitter --> <meta property="twitter:card" content="summary_large_image"> <meta property="twitter:url" content="https://laravel.com/"> <meta property="twitter:title" content="Laravel - The PHP Framework For Web Artisans"> <meta property="twitter:description" content="Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things."> <meta property="twitter:image" content="https://laravel.com/img/og-image.jpg"> <!-- Favicon --> <link rel="apple-touch-icon" sizes="180x180" href="/img/favicon/apple-touch-icon.png"> <link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png"> <link rel="icon" type="image/png" sizes="16x16" href="/img/favicon/favicon-16x16.png"> <link rel="manifest" href="/img/favicon/site.webmanifest"> <link rel="mask-icon" href="/img/favicon/safari-pinned-tab.svg" color="#ff2d20"> <link rel="shortcut icon" href="/img/favicon/favicon.ico"> <meta name="msapplication-TileColor" content="#ff2d20"> <meta name="msapplication-config" content="/img/favicon/browserconfig.xml"> <meta name="theme-color" content="#ffffff"> <meta name="color-scheme" content="light"> <link rel="preconnect" href="https://E3MIRNPJH5-dsn.algolia.net" crossorigin /> <link rel="stylesheet" href="https://use.typekit.net/ins2wgm.css"> <link rel="preload" as="style" href="https://laravel.com/build/assets/app-dc063eeb.css" /><link rel="modulepreload" href="https://laravel.com/build/assets/app-70a5a3a6.js" /><link rel="modulepreload" href="https://laravel.com/build/assets/docs-45166933.js" /><link rel="stylesheet" href="https://laravel.com/build/assets/app-dc063eeb.css" /><script type="module" src="https://laravel.com/build/assets/app-70a5a3a6.js"></script><script type="module" src="https://laravel.com/build/assets/docs-45166933.js"></script> <!-- Fathom - beautiful, simple website analytics --> <script src="https://cdn.usefathom.com/script.js" data-site="DVMEKBYF" defer></script> <!-- / Fathom --> <!-- Clearbit --> <script async src="https://tag.clearbitscripts.com/v1/pk_97d2bf69f817feb07be42fcda1460119/tags.js" referrerpolicy="strict-origin-when-cross-origin"></script> <script> const alwaysLightMode = false; </script> <script> function updateTheme() { if (!('theme' in localStorage)) { localStorage.theme = 'system'; } switch (localStorage.theme) { case 'system': if (window.matchMedia('(prefers-color-scheme: dark)').matches) { document.documentElement.classList.add('dark'); document.documentElement.setAttribute('data-theme', 'dark'); } else { document.documentElement.classList.remove('dark'); document.documentElement.setAttribute('data-theme', 'light'); } document.documentElement.setAttribute('color-theme', 'system'); break; case 'dark': document.documentElement.classList.add('dark'); document.documentElement.setAttribute('color-theme', 'dark'); document.documentElement.setAttribute('data-theme', 'dark'); break; case 'light': document.documentElement.classList.remove('dark'); document.documentElement.setAttribute('color-theme', 'light'); document.documentElement.setAttribute('data-theme', 'light'); break; } updateThemeAndSchemeColor(); } function updateThemeAndSchemeColor() { if (! alwaysLightMode) { if (document.documentElement.classList.contains('dark')) { document.querySelector('meta[name="color-scheme"]').setAttribute('content', 'dark'); document.querySelector('meta[name="theme-color"]').setAttribute('content', '#171923'); return; } document.querySelector('meta[name="color-scheme"]').setAttribute('content', 'light'); document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff'); } } updateTheme(); </script> </head> <body x-data="{ navIsOpen: false, }" class="w-full h-full font-sans antialiased text-gray-900 language-php" data-instant-intensity="0" > <a id="skip-to-content-link" href="#main-content" class="absolute bg-gray-100 px-4 py-2 top-3 left-3 text-gray-700 shadow-xl" > Skip to content </a> <div class="items-center justify-center bg-gradient-to-b from-red-500 to-red-600 p-2 text-center text-white text-sm h-9"> <template id="news-herd"> <div> <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-white" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" stroke-linecap="round" fill="none" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M3 4m0 1a1 1 0 0 1 1 -1h16a1 1 0 0 1 1 1v14a1 1 0 0 1 -1 1h-16a1 1 0 0 1 -1 -1z" /> <path d="M7 8v1" /> <path d="M17 8v1" /> <path d="M12.5 4c-.654 1.486 -1.26 3.443 -1.5 9h2.5c-.19 2.867 .094 5.024 .5 7" /> <path d="M7 15.5c3.667 2 6.333 2 10 0" /> </svg> </div> <div class="mt-px ml-1"> Get started with PHP and Laravel faster than ever using <a href="https://herd.laravel.com" class="underline">Laravel Herd</a>. </div> </template> <template id="news-laracon"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg></div> <div class="mt-px ml-1"> Join us in Dallas, TX! Tickets are now available for <a href="https://laracon.us" class="underline">Laracon US</a>. </div> </template> <template id="news-laracon-in"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg></div> <div class="mt-px ml-1"> Let's go to India! Tickets are now available for <a href="https://laracon.in" class="underline">Laracon IN</a>. </div> </template> <template id="news-laracon-eu"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg></div> <div class="mt-px ml-1"> Let's go to Europe! Tickets are now available for <a href="https://laracon.eu" class="underline">Laracon EU</a>. </div> </template> <template id="news-laracon-au"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg></div> <div class="mt-px ml-1"> Let's go down under! Tickets are now available for <a href="https://laracon.au" class="underline">Laracon AU</a>. </div> </template> <template id="news-forge"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg></div> <div class="mt-px ml-1"> Servers with PHP 8.3 are now available for provisioning via <a href="https://forge.laravel.com" class="underline">Laravel Forge</a>. </div> </template> <template id="news-vapor"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 15a4 4 0 004 4h9a5 5 0 10-.1-9.999 5.002 5.002 0 10-9.78 2.096A4.001 4.001 0 003 15z"></path></svg></div> <div class="mt-px ml-1"> Deploy Laravel with the infinite scale of serverless using <a href="https://vapor.laravel.com" class="underline">Laravel Vapor</a>. </div> </template> <template id="news-cloud"> <div class="mt-px ml-1"> Join the waitlist for <a href="https://cloud.laravel.com" class="underline">Laravel Cloud</a><span class="hidden md:inline">, the future of shipping</span>. </div> </template> <template id="news-nightwatch"> <div class="mt-px ml-1"> Join the waitlist for <a href="https://nightwatch.laravel.com" class="underline">Laravel Nightwatch</a><span class="hidden md:inline">, first-class monitoring designed for Laravel</span>. </div> </template> <template id="news-pulse"> <div><svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 15a4 4 0 004 4h9a5 5 0 10-.1-9.999 5.002 5.002 0 10-9.78 2.096A4.001 4.001 0 003 15z"></path></svg></div> <div class="mt-px ml-1"> How's your health? Check your application's vital signs using <a href="https://pulse.laravel.com" class="underline">Laravel Pulse</a>. </div> </template> <template id="news-reverb"> <div> <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-white" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"/> <path d="M3.707 6.293l2.586 -2.586a1 1 0 0 1 1.414 0l5.586 5.586a1 1 0 0 1 0 1.414l-2.586 2.586a1 1 0 0 1 -1.414 0l-5.586 -5.586a1 1 0 0 1 0 -1.414z" /> <path d="M6 10l-3 3l3 3l3 -3" /> <path d="M10 6l3 -3l3 3l-3 3" /> <path d="M12 12l1.5 1.5" /> <path d="M14.5 17a2.5 2.5 0 0 0 2.5 -2.5" /> <path d="M15 21a6 6 0 0 0 6 -6" /> </svg> </div> <div class="mt-px ml-1"> Incoming transmission received. <a href="https://reverb.laravel.com" class="underline">Laravel Reverb</a> is now available! </div> </template> <template id="news-nova"> <div> <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"> <path stroke-linecap="round" stroke-linejoin="round" d="M15.59 14.37a6 6 0 01-5.84 7.38v-4.8m5.84-2.58a14.98 14.98 0 006.16-12.12A14.98 14.98 0 009.631 8.41m5.96 5.96a14.926 14.926 0 01-5.841 2.58m-.119-8.54a6 6 0 00-7.381 5.84h4.8m2.581-5.84a14.927 14.927 0 00-2.58 5.84m2.699 2.7c-.103.021-.207.041-.311.06a15.09 15.09 0 01-2.448-2.448 14.9 14.9 0 01.06-.312m-2.24 2.39a4.493 4.493 0 00-1.757 4.306 4.493 4.493 0 004.306-1.758M16.5 9a1.5 1.5 0 11-3 0 1.5 1.5 0 013 0z"></path> </svg> </div> <div class="mt-px ml-1"> Take your administration backend to another dimension with <a href="https://nova.laravel.com" class="underline">Laravel Nova</a>. </div> </template> <template id="news-careers"> <div> <svg class="w-5 h-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> <path stroke-linecap="round" stroke-linejoin="round" d="M20.25 14.15v4.25c0 1.094-.787 2.036-1.872 2.18-2.087.277-4.216.42-6.378.42s-4.291-.143-6.378-.42c-1.085-.144-1.872-1.086-1.872-2.18v-4.25m16.5 0a2.18 2.18 0 0 0 .75-1.661V8.706c0-1.081-.768-2.015-1.837-2.175a48.114 48.114 0 0 0-3.413-.387m4.5 8.006c-.194.165-.42.295-.673.38A23.978 23.978 0 0 1 12 15.75c-2.648 0-5.195-.429-7.577-1.22a2.016 2.016 0 0 1-.673-.38m0 0A2.18 2.18 0 0 1 3 12.489V8.706c0-1.081.768-2.015 1.837-2.175a48.111 48.111 0 0 1 3.413-.387m7.5 0V5.25A2.25 2.25 0 0 0 13.5 3h-3a2.25 2.25 0 0 0-2.25 2.25v.894m7.5 0a48.667 48.667 0 0 0-7.5 0M12 12.75h.008v.008H12v-.008Z" /> </svg> </div> <div class="mt-px ml-1"> Laravel is hiring! <a href="https://laravel.com/careers" class="underline">Help us build the future of Laravel</a>. </div> </template> </div> <script> const activeNewsTemplate = document.getElementById( 'news-'+JSON.parse('[\u0022cloud\u0022,\u0022nightwatch\u0022]')[Math.floor(Math.random() * 2)] ) activeNewsTemplate.replaceWith(activeNewsTemplate.content) </script> <div class="relative dark:bg-dark-700" id="docsScreen"> <div class="relative lg:flex lg:items-start"> <aside class="hidden fixed top-0 bottom-0 left-0 z-20 w-16 bg-gradient-to-b from-gray-100 to-white transition-all duration-300 lg:sticky lg:w-80 lg:shrink-0 lg:flex lg:flex-col lg:justify-end lg:items-end 2xl:max-w-lg 2xl:w-full dark:from-dark-800 dark:to-dark-700"> <div class="relative max-h-screen overflow-auto flex-1 flex flex-col xl:w-80"> <a href="/" class="flex items-center py-8 px-4 lg:px-8 xl:px-16"> <img class="w-8 h-8 shrink-0 transition-all duration-300 lg:w-12 lg:h-12" src="/img/logomark.min.svg" alt="Laravel" width="50" height="52" > <img src="/img/logotype.min.svg" alt="Laravel" class="hidden ml-4 lg:block" width="114" height="29" > </a> <div class="px-4 pb-10 lg:px-8 xl:px-16"> <nav id="indexed-nav" class="hidden lg:block lg:mt-4"> <div class="docs_sidebar"> <ul> <li> <h2>Prologue</h2> <ul> <li> <a href="/docs/11.x/releases">Release Notes</a> </li> <li> <a href="/docs/11.x/upgrade">Upgrade Guide</a> </li> <li> <a href="/docs/11.x/contributions">Contribution Guide</a> </li> </ul> </li> <li> <h2>Getting Started</h2> <ul> <li> <a href="/docs/11.x/installation">Installation</a> </li> <li> <a href="/docs/11.x/configuration">Configuration</a> </li> <li> <a href="/docs/11.x/structure">Directory Structure</a> </li> <li> <a href="/docs/11.x/frontend">Frontend</a> </li> <li> <a href="/docs/11.x/starter-kits">Starter Kits</a> </li> <li> <a href="/docs/11.x/deployment">Deployment</a> </li> </ul> </li> <li> <h2>Architecture Concepts</h2> <ul> <li> <a href="/docs/11.x/lifecycle">Request Lifecycle</a> </li> <li> <a href="/docs/11.x/container">Service Container</a> </li> <li> <a href="/docs/11.x/providers">Service Providers</a> </li> <li> <a href="/docs/11.x/facades">Facades</a> </li> </ul> </li> <li> <h2>The Basics</h2> <ul> <li> <a href="/docs/11.x/routing">Routing</a> </li> <li> <a href="/docs/11.x/middleware">Middleware</a> </li> <li> <a href="/docs/11.x/csrf">CSRF Protection</a> </li> <li> <a href="/docs/11.x/controllers">Controllers</a> </li> <li> <a href="/docs/11.x/requests">Requests</a> </li> <li> <a href="/docs/11.x/responses">Responses</a> </li> <li> <a href="/docs/11.x/views">Views</a> </li> <li> <a href="/docs/11.x/blade">Blade Templates</a> </li> <li> <a href="/docs/11.x/vite">Asset Bundling</a> </li> <li> <a href="/docs/11.x/urls">URL Generation</a> </li> <li> <a href="/docs/11.x/session">Session</a> </li> <li> <a href="/docs/11.x/validation">Validation</a> </li> <li> <a href="/docs/11.x/errors">Error Handling</a> </li> <li> <a href="/docs/11.x/logging">Logging</a> </li> </ul> </li> <li class="sub--on"> <h2>Digging Deeper</h2> <ul> <li> <a href="/docs/11.x/artisan">Artisan Console</a> </li> <li> <a href="/docs/11.x/broadcasting">Broadcasting</a> </li> <li> <a href="/docs/11.x/cache">Cache</a> </li> <li> <a href="/docs/11.x/collections">Collections</a> </li> <li> <a href="/docs/11.x/concurrency">Concurrency</a> </li> <li> <a href="/docs/11.x/context">Context</a> </li> <li> <a href="/docs/11.x/contracts">Contracts</a> </li> <li> <a href="/docs/11.x/events">Events</a> </li> <li> <a href="/docs/11.x/filesystem">File Storage</a> </li> <li class="active"> <a href="/docs/11.x/helpers">Helpers</a> </li> <li> <a href="/docs/11.x/http-client">HTTP Client</a> </li> <li> <a href="/docs/11.x/localization">Localization</a> </li> <li> <a href="/docs/11.x/mail">Mail</a> </li> <li> <a href="/docs/11.x/notifications">Notifications</a> </li> <li> <a href="/docs/11.x/packages">Package Development</a> </li> <li> <a href="/docs/11.x/processes">Processes</a> </li> <li> <a href="/docs/11.x/queues">Queues</a> </li> <li> <a href="/docs/11.x/rate-limiting">Rate Limiting</a> </li> <li> <a href="/docs/11.x/strings">Strings</a> </li> <li> <a href="/docs/11.x/scheduling">Task Scheduling</a> </li> </ul> </li> <li> <h2>Security</h2> <ul> <li> <a href="/docs/11.x/authentication">Authentication</a> </li> <li> <a href="/docs/11.x/authorization">Authorization</a> </li> <li> <a href="/docs/11.x/verification">Email Verification</a> </li> <li> <a href="/docs/11.x/encryption">Encryption</a> </li> <li> <a href="/docs/11.x/hashing">Hashing</a> </li> <li> <a href="/docs/11.x/passwords">Password Reset</a> </li> </ul> </li> <li> <h2>Database</h2> <ul> <li> <a href="/docs/11.x/database">Getting Started</a> </li> <li> <a href="/docs/11.x/queries">Query Builder</a> </li> <li> <a href="/docs/11.x/pagination">Pagination</a> </li> <li> <a href="/docs/11.x/migrations">Migrations</a> </li> <li> <a href="/docs/11.x/seeding">Seeding</a> </li> <li> <a href="/docs/11.x/redis">Redis</a> </li> <li> <a href="/docs/11.x/mongodb">MongoDB</a> </li> </ul> </li> <li> <h2>Eloquent ORM</h2> <ul> <li> <a href="/docs/11.x/eloquent">Getting Started</a> </li> <li> <a href="/docs/11.x/eloquent-relationships">Relationships</a> </li> <li> <a href="/docs/11.x/eloquent-collections">Collections</a> </li> <li> <a href="/docs/11.x/eloquent-mutators">Mutators / Casts</a> </li> <li> <a href="/docs/11.x/eloquent-resources">API Resources</a> </li> <li> <a href="/docs/11.x/eloquent-serialization">Serialization</a> </li> <li> <a href="/docs/11.x/eloquent-factories">Factories</a> </li> </ul> </li> <li> <h2>Testing</h2> <ul> <li> <a href="/docs/11.x/testing">Getting Started</a> </li> <li> <a href="/docs/11.x/http-tests">HTTP Tests</a> </li> <li> <a href="/docs/11.x/console-tests">Console Tests</a> </li> <li> <a href="/docs/11.x/dusk">Browser Tests</a> </li> <li> <a href="/docs/11.x/database-testing">Database</a> </li> <li> <a href="/docs/11.x/mocking">Mocking</a> </li> </ul> </li> <li> <h2>Packages</h2> <ul> <li> <a href="/docs/11.x/starter-kits#laravel-breeze">Breeze</a> </li> <li> <a href="/docs/11.x/billing">Cashier (Stripe)</a> </li> <li> <a href="/docs/11.x/cashier-paddle">Cashier (Paddle)</a> </li> <li> <a href="/docs/11.x/dusk">Dusk</a> </li> <li> <a href="/docs/11.x/envoy">Envoy</a> </li> <li> <a href="/docs/11.x/fortify">Fortify</a> </li> <li> <a href="/docs/11.x/folio">Folio</a> </li> <li> <a href="/docs/11.x/homestead">Homestead</a> </li> <li> <a href="/docs/11.x/horizon">Horizon</a> </li> <li> <a href="https://jetstream.laravel.com">Jetstream</a> </li> <li> <a href="/docs/11.x/mix">Mix</a> </li> <li> <a href="/docs/11.x/octane">Octane</a> </li> <li> <a href="/docs/11.x/passport">Passport</a> </li> <li> <a href="/docs/11.x/pennant">Pennant</a> </li> <li> <a href="/docs/11.x/pint">Pint</a> </li> <li> <a href="/docs/11.x/precognition">Precognition</a> </li> <li> <a href="/docs/11.x/prompts">Prompts</a> </li> <li> <a href="/docs/11.x/pulse">Pulse</a> </li> <li> <a href="/docs/11.x/reverb">Reverb</a> </li> <li> <a href="/docs/11.x/sail">Sail</a> </li> <li> <a href="/docs/11.x/sanctum">Sanctum</a> </li> <li> <a href="/docs/11.x/scout">Scout</a> </li> <li> <a href="/docs/11.x/socialite">Socialite</a> </li> <li> <a href="/docs/11.x/telescope">Telescope</a> </li> <li> <a href="/docs/11.x/valet">Valet</a> </li> </ul> </li> <li> <a href="/api/11.x">API Documentation</a> </li> </ul> </div> </nav> <template id="promote-forge"> <div class="mt-4 px-3 py-2 border-dashed border-gray-200 border rounded-lg text-xs leading-loose text-gray-700 lg:block dark:border-gray-400 dark:text-gray-200"> <span class="font-medium">Laravel Forge:</span> create and manage PHP 8 servers. Deploy your Laravel applications in seconds. <a class="underline text-red-600" href="https://forge.laravel.com">Sign up now!</a>. </div> </template> <template id="promote-vapor"> <div class="mt-4 px-3 py-2 border-dashed border-gray-200 border rounded-lg text-xs leading-loose text-gray-700 lg:block dark:border-gray-400 dark:text-gray-200"> <span class="font-medium">Laravel Vapor:</span> experience extreme scale on a dedicated serverless platform for Laravel. <a class="underline text-red-600" href="https://vapor.laravel.com">Sign up now!</a>. </div> </template> <template id="promote-nova"> <div class="mt-4 px-3 py-2 border-dashed border-gray-200 border rounded-lg text-xs leading-loose text-gray-700 lg:block dark:border-gray-400 dark:text-gray-200"> <span class="font-medium">Laravel Nova:</span> The next generation of Nova is <a class="underline text-red-600" href="https://nova.laravel.com">now available</a>. </div> </template> <template id="promote-pulse"> <div class="mt-4 px-3 py-2 border-dashed border-gray-200 border rounded-lg text-xs leading-loose text-gray-700 lg:block dark:border-gray-400 dark:text-gray-200"> <span class="font-medium">Laravel Pulse:</span> How's your health? Check your application's vital signs using <a href="https://pulse.laravel.com" class="underline text-red-600">Laravel Pulse</a>. </div> </template> <template id="promote-reverb"> <div class="mt-4 px-3 py-2 border-dashed border-gray-200 border rounded-lg text-xs leading-loose text-gray-700 lg:block dark:border-gray-400 dark:text-gray-200"> <span class="font-medium">Laravel Reverb:</span> You can easily build dynamic, real-time Laravel applications using WebSockets. <a href="https://reverb.laravel.com" class="underline text-red-600">Laravel Reverb</a> is now available! </div> </template> <script> const activePromotionTemplate = document.getElementById( 'promote-'+JSON.parse('[\u0022forge\u0022,\u0022vapor\u0022,\u0022nova\u0022,\u0022pulse\u0022,\u0022reverb\u0022]')[Math.floor(Math.random() * 5)] ) activePromotionTemplate.replaceWith(activePromotionTemplate.content) </script> </div> </div> </aside> <header class="lg:hidden" @keydown.window.escape="navIsOpen = false" @click.away="navIsOpen = false" > <div class="relative mx-auto w-full py-10 bg-white transition duration-200 dark:bg-dark-700"> <div class="mx-auto px-8 sm:px-16 flex items-center justify-between"> <a href="/" class="flex items-center"> <img class="" src="/img/logomark.min.svg" alt="Laravel" width="50" height="52"> <img class="hidden ml-5 sm:block" src="/img/logotype.min.svg" alt="Laravel" width="114" height="29"> </a> <div class="flex-1 flex items-center justify-end"> <button id="header__sun" onclick="toSystemMode()" title="Switch to system theme" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-sun" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"></path> <circle cx="12" cy="12" r="4"></circle> <path d="M3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7"></path> </svg> </button> <button id="header__moon" onclick="toLightMode()" title="Switch to light mode" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fill="currentColor" d="M17.75,4.09L15.22,6.03L16.13,9.09L13.5,7.28L10.87,9.09L11.78,6.03L9.25,4.09L12.44,4L13.5,1L14.56,4L17.75,4.09M21.25,11L19.61,12.25L20.2,14.23L18.5,13.06L16.8,14.23L17.39,12.25L15.75,11L17.81,10.95L18.5,9L19.19,10.95L21.25,11M18.97,15.95C19.8,15.87 20.69,17.05 20.16,17.8C19.84,18.25 19.5,18.67 19.08,19.07C15.17,23 8.84,23 4.94,19.07C1.03,15.17 1.03,8.83 4.94,4.93C5.34,4.53 5.76,4.17 6.21,3.85C6.96,3.32 8.14,4.21 8.06,5.04C7.79,7.9 8.75,10.87 10.95,13.06C13.14,15.26 16.1,16.22 18.97,15.95M17.33,17.97C14.5,17.81 11.7,16.64 9.53,14.5C7.36,12.31 6.2,9.5 6.04,6.68C3.23,9.82 3.34,14.64 6.35,17.66C9.37,20.67 14.19,20.78 17.33,17.97Z" /> </svg> </button> <button id="header__indeterminate" onclick="toDarkMode()" title="Switch to dark mode" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fill="currentColor" d="M12 2A10 10 0 0 0 2 12A10 10 0 0 0 12 22A10 10 0 0 0 22 12A10 10 0 0 0 12 2M12 4A8 8 0 0 1 20 12A8 8 0 0 1 12 20V4Z" /> </svg> </button> <button class="ml-2 relative w-10 h-10 p-2 text-red-600 lg:hidden focus:outline-none focus:shadow-outline" aria-label="Menu" @click.prevent="navIsOpen = !navIsOpen"> <svg x-show="! navIsOpen" x-transition.opacity class="absolute inset-0 mt-2 ml-2 w-6 h-6" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg> <svg x-show="navIsOpen" x-transition.opacity x-cloak class="absolute inset-0 mt-2 ml-2 w-6 h-6" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg> </button> </div> </div> <span :class="{ 'shadow-sm': navIsOpen }" class="absolute inset-0 z-20 pointer-events-none"></span> </div> <div x-show="navIsOpen" x-transition:enter="duration-150" x-transition:leave="duration-100 ease-in" x-cloak > <nav x-show="navIsOpen" x-cloak class="absolute w-full transform origin-top shadow-sm z-10" x-transition:enter="duration-150 ease-out" x-transition:enter-start="opacity-0 -translate-y-8 scale-75" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="duration-100 ease-in" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 -translate-y-8 scale-75" > <div class="relative p-8 bg-white docs_sidebar dark:bg-dark-600"> <ul> <li> <h2>Prologue</h2> <ul> <li> <a href="/docs/11.x/releases">Release Notes</a> </li> <li> <a href="/docs/11.x/upgrade">Upgrade Guide</a> </li> <li> <a href="/docs/11.x/contributions">Contribution Guide</a> </li> </ul> </li> <li> <h2>Getting Started</h2> <ul> <li> <a href="/docs/11.x/installation">Installation</a> </li> <li> <a href="/docs/11.x/configuration">Configuration</a> </li> <li> <a href="/docs/11.x/structure">Directory Structure</a> </li> <li> <a href="/docs/11.x/frontend">Frontend</a> </li> <li> <a href="/docs/11.x/starter-kits">Starter Kits</a> </li> <li> <a href="/docs/11.x/deployment">Deployment</a> </li> </ul> </li> <li> <h2>Architecture Concepts</h2> <ul> <li> <a href="/docs/11.x/lifecycle">Request Lifecycle</a> </li> <li> <a href="/docs/11.x/container">Service Container</a> </li> <li> <a href="/docs/11.x/providers">Service Providers</a> </li> <li> <a href="/docs/11.x/facades">Facades</a> </li> </ul> </li> <li> <h2>The Basics</h2> <ul> <li> <a href="/docs/11.x/routing">Routing</a> </li> <li> <a href="/docs/11.x/middleware">Middleware</a> </li> <li> <a href="/docs/11.x/csrf">CSRF Protection</a> </li> <li> <a href="/docs/11.x/controllers">Controllers</a> </li> <li> <a href="/docs/11.x/requests">Requests</a> </li> <li> <a href="/docs/11.x/responses">Responses</a> </li> <li> <a href="/docs/11.x/views">Views</a> </li> <li> <a href="/docs/11.x/blade">Blade Templates</a> </li> <li> <a href="/docs/11.x/vite">Asset Bundling</a> </li> <li> <a href="/docs/11.x/urls">URL Generation</a> </li> <li> <a href="/docs/11.x/session">Session</a> </li> <li> <a href="/docs/11.x/validation">Validation</a> </li> <li> <a href="/docs/11.x/errors">Error Handling</a> </li> <li> <a href="/docs/11.x/logging">Logging</a> </li> </ul> </li> <li class="sub--on"> <h2>Digging Deeper</h2> <ul> <li> <a href="/docs/11.x/artisan">Artisan Console</a> </li> <li> <a href="/docs/11.x/broadcasting">Broadcasting</a> </li> <li> <a href="/docs/11.x/cache">Cache</a> </li> <li> <a href="/docs/11.x/collections">Collections</a> </li> <li> <a href="/docs/11.x/concurrency">Concurrency</a> </li> <li> <a href="/docs/11.x/context">Context</a> </li> <li> <a href="/docs/11.x/contracts">Contracts</a> </li> <li> <a href="/docs/11.x/events">Events</a> </li> <li> <a href="/docs/11.x/filesystem">File Storage</a> </li> <li class="active"> <a href="/docs/11.x/helpers">Helpers</a> </li> <li> <a href="/docs/11.x/http-client">HTTP Client</a> </li> <li> <a href="/docs/11.x/localization">Localization</a> </li> <li> <a href="/docs/11.x/mail">Mail</a> </li> <li> <a href="/docs/11.x/notifications">Notifications</a> </li> <li> <a href="/docs/11.x/packages">Package Development</a> </li> <li> <a href="/docs/11.x/processes">Processes</a> </li> <li> <a href="/docs/11.x/queues">Queues</a> </li> <li> <a href="/docs/11.x/rate-limiting">Rate Limiting</a> </li> <li> <a href="/docs/11.x/strings">Strings</a> </li> <li> <a href="/docs/11.x/scheduling">Task Scheduling</a> </li> </ul> </li> <li> <h2>Security</h2> <ul> <li> <a href="/docs/11.x/authentication">Authentication</a> </li> <li> <a href="/docs/11.x/authorization">Authorization</a> </li> <li> <a href="/docs/11.x/verification">Email Verification</a> </li> <li> <a href="/docs/11.x/encryption">Encryption</a> </li> <li> <a href="/docs/11.x/hashing">Hashing</a> </li> <li> <a href="/docs/11.x/passwords">Password Reset</a> </li> </ul> </li> <li> <h2>Database</h2> <ul> <li> <a href="/docs/11.x/database">Getting Started</a> </li> <li> <a href="/docs/11.x/queries">Query Builder</a> </li> <li> <a href="/docs/11.x/pagination">Pagination</a> </li> <li> <a href="/docs/11.x/migrations">Migrations</a> </li> <li> <a href="/docs/11.x/seeding">Seeding</a> </li> <li> <a href="/docs/11.x/redis">Redis</a> </li> <li> <a href="/docs/11.x/mongodb">MongoDB</a> </li> </ul> </li> <li> <h2>Eloquent ORM</h2> <ul> <li> <a href="/docs/11.x/eloquent">Getting Started</a> </li> <li> <a href="/docs/11.x/eloquent-relationships">Relationships</a> </li> <li> <a href="/docs/11.x/eloquent-collections">Collections</a> </li> <li> <a href="/docs/11.x/eloquent-mutators">Mutators / Casts</a> </li> <li> <a href="/docs/11.x/eloquent-resources">API Resources</a> </li> <li> <a href="/docs/11.x/eloquent-serialization">Serialization</a> </li> <li> <a href="/docs/11.x/eloquent-factories">Factories</a> </li> </ul> </li> <li> <h2>Testing</h2> <ul> <li> <a href="/docs/11.x/testing">Getting Started</a> </li> <li> <a href="/docs/11.x/http-tests">HTTP Tests</a> </li> <li> <a href="/docs/11.x/console-tests">Console Tests</a> </li> <li> <a href="/docs/11.x/dusk">Browser Tests</a> </li> <li> <a href="/docs/11.x/database-testing">Database</a> </li> <li> <a href="/docs/11.x/mocking">Mocking</a> </li> </ul> </li> <li> <h2>Packages</h2> <ul> <li> <a href="/docs/11.x/starter-kits#laravel-breeze">Breeze</a> </li> <li> <a href="/docs/11.x/billing">Cashier (Stripe)</a> </li> <li> <a href="/docs/11.x/cashier-paddle">Cashier (Paddle)</a> </li> <li> <a href="/docs/11.x/dusk">Dusk</a> </li> <li> <a href="/docs/11.x/envoy">Envoy</a> </li> <li> <a href="/docs/11.x/fortify">Fortify</a> </li> <li> <a href="/docs/11.x/folio">Folio</a> </li> <li> <a href="/docs/11.x/homestead">Homestead</a> </li> <li> <a href="/docs/11.x/horizon">Horizon</a> </li> <li> <a href="https://jetstream.laravel.com">Jetstream</a> </li> <li> <a href="/docs/11.x/mix">Mix</a> </li> <li> <a href="/docs/11.x/octane">Octane</a> </li> <li> <a href="/docs/11.x/passport">Passport</a> </li> <li> <a href="/docs/11.x/pennant">Pennant</a> </li> <li> <a href="/docs/11.x/pint">Pint</a> </li> <li> <a href="/docs/11.x/precognition">Precognition</a> </li> <li> <a href="/docs/11.x/prompts">Prompts</a> </li> <li> <a href="/docs/11.x/pulse">Pulse</a> </li> <li> <a href="/docs/11.x/reverb">Reverb</a> </li> <li> <a href="/docs/11.x/sail">Sail</a> </li> <li> <a href="/docs/11.x/sanctum">Sanctum</a> </li> <li> <a href="/docs/11.x/scout">Scout</a> </li> <li> <a href="/docs/11.x/socialite">Socialite</a> </li> <li> <a href="/docs/11.x/telescope">Telescope</a> </li> <li> <a href="/docs/11.x/valet">Valet</a> </li> </ul> </li> <li> <a href="/api/11.x">API Documentation</a> </li> </ul> </div> </nav> </div> </header> <section class="flex-1 dark:bg-dark-700"> <div class="max-w-screen-lg px-8 sm:px-16 lg:px-24"> <div class="flex flex-col items-end border-b border-gray-200 py-1 transition-colors dark:border-gray-700 lg:mt-8 lg:flex-row-reverse"> <div class="hidden lg:flex items-center justify-center ml-8"> <button id="header__sun" onclick="toSystemMode()" title="Switch to system theme" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-sun" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none"></path> <circle cx="12" cy="12" r="4"></circle> <path d="M3 12h1m8 -9v1m8 8h1m-9 8v1m-6.4 -15.4l.7 .7m12.1 -.7l-.7 .7m0 11.4l.7 .7m-12.1 -.7l-.7 .7"></path> </svg> </button> <button id="header__moon" onclick="toLightMode()" title="Switch to light mode" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fill="currentColor" d="M17.75,4.09L15.22,6.03L16.13,9.09L13.5,7.28L10.87,9.09L11.78,6.03L9.25,4.09L12.44,4L13.5,1L14.56,4L17.75,4.09M21.25,11L19.61,12.25L20.2,14.23L18.5,13.06L16.8,14.23L17.39,12.25L15.75,11L17.81,10.95L18.5,9L19.19,10.95L21.25,11M18.97,15.95C19.8,15.87 20.69,17.05 20.16,17.8C19.84,18.25 19.5,18.67 19.08,19.07C15.17,23 8.84,23 4.94,19.07C1.03,15.17 1.03,8.83 4.94,4.93C5.34,4.53 5.76,4.17 6.21,3.85C6.96,3.32 8.14,4.21 8.06,5.04C7.79,7.9 8.75,10.87 10.95,13.06C13.14,15.26 16.1,16.22 18.97,15.95M17.33,17.97C14.5,17.81 11.7,16.64 9.53,14.5C7.36,12.31 6.2,9.5 6.04,6.68C3.23,9.82 3.34,14.64 6.35,17.66C9.37,20.67 14.19,20.78 17.33,17.97Z" /> </svg> </button> <button id="header__indeterminate" onclick="toDarkMode()" title="Switch to dark mode" class="relative w-10 h-10 focus:outline-none focus:shadow-outline text-gray-500"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fill="currentColor" d="M12 2A10 10 0 0 0 2 12A10 10 0 0 0 12 22A10 10 0 0 0 22 12A10 10 0 0 0 12 2M12 4A8 8 0 0 1 20 12A8 8 0 0 1 12 20V4Z" /> </svg> </button> </div> <div class="w-full lg:w-40 lg:pl-12"> <div> <label class="text-gray-600 text-xs tracking-widest uppercase dark:text-gray-500" for="version-switcher">Version</label> <div x-data class="relative w-full bg-white transition-all duration-500 focus-within:border-gray-600 dark:bg-gray-800"> <select id="version-switcher" aria-label="Laravel version" class="appearance-none flex-1 w-full px-0 py-1 placeholder-gray-900 tracking-wide bg-white border-transparent focus:outline-none dark:bg-dark-700 dark:text-gray-400 dark:placeholder-gray-500" @change="window.location = $event.target.value" > <option value="https://laravel.com/docs/master/helpers">Master</option> <option selected value="https://laravel.com/docs/11.x/helpers">11.x</option> <option value="https://laravel.com/docs/10.x/helpers">10.x</option> <option value="https://laravel.com/docs/9.x/helpers">9.x</option> <option value="https://laravel.com/docs/8.x/helpers">8.x</option> <option value="https://laravel.com/docs/7.x/helpers">7.x</option> <option value="https://laravel.com/docs/6.x/helpers">6.x</option> <option value="https://laravel.com/docs/5.8/helpers">5.8</option> <option value="https://laravel.com/docs/5.7/helpers">5.7</option> <option value="https://laravel.com/docs/5.6/helpers">5.6</option> <option value="https://laravel.com/docs/5.5/helpers">5.5</option> <option value="https://laravel.com/docs/5.4/helpers">5.4</option> <option value="https://laravel.com/docs/5.3/helpers">5.3</option> <option value="https://laravel.com/docs/5.2/helpers">5.2</option> <option value="https://laravel.com/docs/5.1/helpers">5.1</option> <option value="https://laravel.com/docs/5.0/helpers">5.0</option> <option value="https://laravel.com/docs/4.2/helpers">4.2</option> </select> <img class="absolute inset-y-0 right-0 mt-2.5 w-2.5 h-2.5 text-gray-900 pointer-events-none dark:hidden" src="/img/icons/drop_arrow.min.svg" alt="" width="10" height="10"> <img class="absolute inset-y-0 right-0 mt-2.5 w-2.5 h-2.5 text-gray-900 pointer-events-none hidden dark:block" src="/img/icons/drop_arrow.dark.min.svg" alt="" width="10" height="10"> </div> </div> </div> <div class="relative mt-8 flex items-center justify-end w-full h-10 lg:mt-0"> <div class="flex-1 flex items-center"> <button id="docsearch" class="text-gray-800 transition-colors dark:text-gray-400 w-full"></button> </div> </div> </div> <section class="mt-8 md:mt-16"> <section class="docs_main max-w-prose"> <div id="main-content"> <h1>Helpers</h1> <ul> <li> <a href="#introduction">Introduction</a> </li> <li> <a href="#available-methods">Available Methods</a> </li> <li> <a href="#other-utilities">Other Utilities</a> <ul> <li> <a href="#benchmarking">Benchmarking</a> </li> <li> <a href="#dates">Dates</a> </li> <li> <a href="#deferred-functions">Deferred Functions</a> </li> <li> <a href="#lottery">Lottery</a> </li> <li> <a href="#pipeline">Pipeline</a> </li> <li> <a href="#sleep">Sleep</a> </li> </ul> </li> </ul> <h2 id="introduction"><a href="#introduction">Introduction</a></h2> <p>Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.</p> <h2 id="available-methods"><a href="#available-methods">Available Methods</a></h2> <style> .collection-method-list > p { columns: 10.8em 3; -moz-columns: 10.8em 3; -webkit-columns: 10.8em 3; } .collection-method-list a { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } </style> <h3 id="arrays-and-objects-method-list"><a href="#arrays-and-objects-method-list">Arrays & Objects</a></h3> <div class="collection-method-list" markdown="1"> <p><a href="#method-array-accessible">Arr::accessible</a> <a href="#method-array-add">Arr::add</a> <a href="#method-array-collapse">Arr::collapse</a> <a href="#method-array-crossjoin">Arr::crossJoin</a> <a href="#method-array-divide">Arr::divide</a> <a href="#method-array-dot">Arr::dot</a> <a href="#method-array-except">Arr::except</a> <a href="#method-array-exists">Arr::exists</a> <a href="#method-array-first">Arr::first</a> <a href="#method-array-flatten">Arr::flatten</a> <a href="#method-array-forget">Arr::forget</a> <a href="#method-array-get">Arr::get</a> <a href="#method-array-has">Arr::has</a> <a href="#method-array-hasany">Arr::hasAny</a> <a href="#method-array-isassoc">Arr::isAssoc</a> <a href="#method-array-islist">Arr::isList</a> <a href="#method-array-join">Arr::join</a> <a href="#method-array-keyby">Arr::keyBy</a> <a href="#method-array-last">Arr::last</a> <a href="#method-array-map">Arr::map</a> <a href="#method-array-map-spread">Arr::mapSpread</a> <a href="#method-array-map-with-keys">Arr::mapWithKeys</a> <a href="#method-array-only">Arr::only</a> <a href="#method-array-pluck">Arr::pluck</a> <a href="#method-array-prepend">Arr::prepend</a> <a href="#method-array-prependkeyswith">Arr::prependKeysWith</a> <a href="#method-array-pull">Arr::pull</a> <a href="#method-array-query">Arr::query</a> <a href="#method-array-random">Arr::random</a> <a href="#method-array-set">Arr::set</a> <a href="#method-array-shuffle">Arr::shuffle</a> <a href="#method-array-sort">Arr::sort</a> <a href="#method-array-sort-desc">Arr::sortDesc</a> <a href="#method-array-sort-recursive">Arr::sortRecursive</a> <a href="#method-array-take">Arr::take</a> <a href="#method-array-to-css-classes">Arr::toCssClasses</a> <a href="#method-array-to-css-styles">Arr::toCssStyles</a> <a href="#method-array-undot">Arr::undot</a> <a href="#method-array-where">Arr::where</a> <a href="#method-array-where-not-null">Arr::whereNotNull</a> <a href="#method-array-wrap">Arr::wrap</a> <a href="#method-data-fill">data_fill</a> <a href="#method-data-get">data_get</a> <a href="#method-data-set">data_set</a> <a href="#method-data-forget">data_forget</a> <a href="#method-head">head</a> <a href="#method-last">last</a></p> </div> <h3 id="numbers-method-list"><a href="#numbers-method-list">Numbers</a></h3> <div class="collection-method-list" markdown="1"> <p><a href="#method-number-abbreviate">Number::abbreviate</a> <a href="#method-number-clamp">Number::clamp</a> <a href="#method-number-currency">Number::currency</a> <a href="#method-default-currency">Number::defaultCurrency</a> <a href="#method-default-locale">Number::defaultLocale</a> <a href="#method-number-file-size">Number::fileSize</a> <a href="#method-number-for-humans">Number::forHumans</a> <a href="#method-number-format">Number::format</a> <a href="#method-number-ordinal">Number::ordinal</a> <a href="#method-number-pairs">Number::pairs</a> <a href="#method-number-percentage">Number::percentage</a> <a href="#method-number-spell">Number::spell</a> <a href="#method-number-trim">Number::trim</a> <a href="#method-number-use-locale">Number::useLocale</a> <a href="#method-number-with-locale">Number::withLocale</a> <a href="#method-number-use-currency">Number::useCurrency</a> <a href="#method-number-with-currency">Number::withCurrency</a></p> </div> <h3 id="paths-method-list"><a href="#paths-method-list">Paths</a></h3> <div class="collection-method-list" markdown="1"> <p><a href="#method-app-path">app_path</a> <a href="#method-base-path">base_path</a> <a href="#method-config-path">config_path</a> <a href="#method-database-path">database_path</a> <a href="#method-lang-path">lang_path</a> <a href="#method-mix">mix</a> <a href="#method-public-path">public_path</a> <a href="#method-resource-path">resource_path</a> <a href="#method-storage-path">storage_path</a></p> </div> <h3 id="urls-method-list"><a href="#urls-method-list">URLs</a></h3> <div class="collection-method-list" markdown="1"> <p><a href="#method-action">action</a> <a href="#method-asset">asset</a> <a href="#method-route">route</a> <a href="#method-secure-asset">secure_asset</a> <a href="#method-secure-url">secure_url</a> <a href="#method-to-route">to_route</a> <a href="#method-url">url</a></p> </div> <h3 id="miscellaneous-method-list"><a href="#miscellaneous-method-list">Miscellaneous</a></h3> <div class="collection-method-list" markdown="1"> <p><a href="#method-abort">abort</a> <a href="#method-abort-if">abort_if</a> <a href="#method-abort-unless">abort_unless</a> <a href="#method-app">app</a> <a href="#method-auth">auth</a> <a href="#method-back">back</a> <a href="#method-bcrypt">bcrypt</a> <a href="#method-blank">blank</a> <a href="#method-broadcast">broadcast</a> <a href="#method-cache">cache</a> <a href="#method-class-uses-recursive">class_uses_recursive</a> <a href="#method-collect">collect</a> <a href="#method-config">config</a> <a href="#method-context">context</a> <a href="#method-cookie">cookie</a> <a href="#method-csrf-field">csrf_field</a> <a href="#method-csrf-token">csrf_token</a> <a href="#method-decrypt">decrypt</a> <a href="#method-dd">dd</a> <a href="#method-dispatch">dispatch</a> <a href="#method-dispatch-sync">dispatch_sync</a> <a href="#method-dump">dump</a> <a href="#method-encrypt">encrypt</a> <a href="#method-env">env</a> <a href="#method-event">event</a> <a href="#method-fake">fake</a> <a href="#method-filled">filled</a> <a href="#method-info">info</a> <a href="#method-literal">literal</a> <a href="#method-logger">logger</a> <a href="#method-method-field">method_field</a> <a href="#method-now">now</a> <a href="#method-old">old</a> <a href="#method-once">once</a> <a href="#method-optional">optional</a> <a href="#method-policy">policy</a> <a href="#method-redirect">redirect</a> <a href="#method-report">report</a> <a href="#method-report-if">report_if</a> <a href="#method-report-unless">report_unless</a> <a href="#method-request">request</a> <a href="#method-rescue">rescue</a> <a href="#method-resolve">resolve</a> <a href="#method-response">response</a> <a href="#method-retry">retry</a> <a href="#method-session">session</a> <a href="#method-tap">tap</a> <a href="#method-throw-if">throw_if</a> <a href="#method-throw-unless">throw_unless</a> <a href="#method-today">today</a> <a href="#method-trait-uses-recursive">trait_uses_recursive</a> <a href="#method-transform">transform</a> <a href="#method-validator">validator</a> <a href="#method-value">value</a> <a href="#method-view">view</a> <a href="#method-with">with</a> <a href="#method-when">when</a></p> </div> <h2 id="arrays"><a href="#arrays">Arrays & Objects</a></h2> <h4 id="method-array-accessible" class="collection-method first-collection-method"><a href="#method-array-accessible"><code>Arr::accessible()</code></a></h4> <p>The <code>Arr::accessible</code> method determines if the given value is array accessible:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAccessible</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">accessible</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAccessible</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">accessible</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAccessible</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">accessible</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">abc</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAccessible</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">accessible</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">stdClass</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-add" class="collection-method"><a href="#method-array-add"><code>Arr::add()</code></a></h4> <p>The <code>Arr::add</code> method adds a given key / value pair to an array if the given key doesn't already exist in the array or is set to <code>null</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">add</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Desk', 'price' => 100]</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">add</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">], </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Desk', 'price' => 100]</span></div></code></pre> <h4 id="method-array-collapse" class="collection-method"><a href="#method-array-collapse"><code>Arr::collapse()</code></a></h4> <p>The <code>Arr::collapse</code> method collapses an array of arrays into a single array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">collapse</span><span style="color: #BFC7D5;">([[</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">], [</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">], [</span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">]]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></div></code></pre> <h4 id="method-array-crossjoin" class="collection-method"><a href="#method-array-crossjoin"><code>Arr::crossJoin()</code></a></h4> <p>The <code>Arr::crossJoin</code> method cross joins the given arrays, returning a Cartesian product with all possible permutations:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$matrix</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">crossJoin</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">], [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> [1, 'a'],</span></div><div class='line'><span style="color: #697098;"> [1, 'b'],</span></div><div class='line'><span style="color: #697098;"> [2, 'a'],</span></div><div class='line'><span style="color: #697098;"> [2, 'b'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$matrix</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">crossJoin</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">], [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">a</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">I</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">II</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> [1, 'a', 'I'],</span></div><div class='line'><span style="color: #697098;"> [1, 'a', 'II'],</span></div><div class='line'><span style="color: #697098;"> [1, 'b', 'I'],</span></div><div class='line'><span style="color: #697098;"> [1, 'b', 'II'],</span></div><div class='line'><span style="color: #697098;"> [2, 'a', 'I'],</span></div><div class='line'><span style="color: #697098;"> [2, 'a', 'II'],</span></div><div class='line'><span style="color: #697098;"> [2, 'b', 'I'],</span></div><div class='line'><span style="color: #697098;"> [2, 'b', 'II'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-divide" class="collection-method"><a href="#method-array-divide"><code>Arr::divide()</code></a></h4> <p>The <code>Arr::divide</code> method returns two arrays: one containing the keys and the other containing the values of the given array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$keys</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$values</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">divide</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> $keys: ['name']</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> $values: ['Desk']</span></div></code></pre> <h4 id="method-array-dot" class="collection-method"><a href="#method-array-dot"><code>Arr::dot()</code></a></h4> <p>The <code>Arr::dot</code> method flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$flattened</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dot</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products.desk.price' => 100]</span></div></code></pre> <h4 id="method-array-except" class="collection-method"><a href="#method-array-except"><code>Arr::except()</code></a></h4> <p>The <code>Arr::except</code> method removes the given key / value pairs from an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">except</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Desk']</span></div></code></pre> <h4 id="method-array-exists" class="collection-method"><a href="#method-array-exists"><code>Arr::exists()</code></a></h4> <p>The <code>Arr::exists</code> method checks that the given key exists in the provided array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">17</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$exists</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">exists</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$exists</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">exists</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">salary</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-first" class="collection-method"><a href="#method-array-first"><code>Arr::first()</code></a></h4> <p>The <code>Arr::first</code> method returns the first element of an array passing a given truth test:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$first</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 200</span></div></code></pre> <p>A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$first</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$callback</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-array-flatten" class="collection-method"><a href="#method-array-flatten"><code>Arr::flatten()</code></a></h4> <p>The <code>Arr::flatten</code> method flattens a multi-dimensional array into a single level array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Joe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">languages</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">PHP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Ruby</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$flattened</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">flatten</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Joe', 'PHP', 'Ruby']</span></div></code></pre> <h4 id="method-array-forget" class="collection-method"><a href="#method-array-forget"><code>Arr::forget()</code></a></h4> <p>The <code>Arr::forget</code> method removes a given key / value pair from a deeply nested array using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forget</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => []]</span></div></code></pre> <h4 id="method-array-get" class="collection-method"><a href="#method-array-get"><code>Arr::get()</code></a></h4> <p>The <code>Arr::get</code> method retrieves a value from a deeply nested array using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100</span></div></code></pre> <p>The <code>Arr::get</code> method also accepts a default value, which will be returned if the specified key is not present in the array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$discount</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 0</span></div></code></pre> <h4 id="method-array-has" class="collection-method"><a href="#method-array-has"><code>Arr::has()</code></a></h4> <p>The <code>Arr::has</code> method checks whether a given item or items exists in an array using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$contains</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$contains</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-hasany" class="collection-method"><a href="#method-array-hasany"><code>Arr::hasAny()</code></a></h4> <p>The <code>Arr::hasAny</code> method checks whether any item in a given set exists in an array using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$contains</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$contains</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$contains</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">category</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-isassoc" class="collection-method"><a href="#method-array-isassoc"><code>Arr::isAssoc()</code></a></h4> <p>The <code>Arr::isAssoc</code> method returns <code>true</code> if the given array is an associative array. An array is considered "associative" if it doesn't have sequential numerical keys beginning with zero:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAssoc</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">isAssoc</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isAssoc</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">isAssoc</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-islist" class="collection-method"><a href="#method-array-islist"><code>Arr::isList()</code></a></h4> <p>The <code>Arr::isList</code> method returns <code>true</code> if the given array's keys are sequential integers beginning from zero:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isList</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">isList</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">bar</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">baz</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isList</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">isList</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <h4 id="method-array-join" class="collection-method"><a href="#method-array-join"><code>Arr::join()</code></a></h4> <p>The <code>Arr::join</code> method joins array elements with a string. Using this method's second argument, you may also specify the joining string for the final element of the array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Tailwind</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Alpine</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Laravel</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Livewire</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$joined</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Tailwind, Alpine, Laravel, Livewire</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$joined</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> and </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Tailwind, Alpine, Laravel and Livewire</span></div></code></pre> <h4 id="method-array-keyby" class="collection-method"><a href="#method-array-keyby"><code>Arr::keyBy()</code></a></h4> <p>The <code>Arr::keyBy</code> method keys the array by the given key. If multiple items have the same key, only the last one will appear in the new array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">keyBy</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],</span></div><div class='line'><span style="color: #697098;"> 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-last" class="collection-method"><a href="#method-array-last"><code>Arr::last()</code></a></h4> <p>The <code>Arr::last</code> method returns the last element of an array passing a given truth test:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">110</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$last</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 300</span></div></code></pre> <p>A default value may be passed as the third argument to the method. This value will be returned if no value passes the truth test:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$last</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$callback</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-array-map" class="collection-method"><a href="#method-array-map"><code>Arr::map()</code></a></h4> <p>The <code>Arr::map</code> method iterates through the array and passes each value and key to the given callback. The array value is replaced by the value returned by the callback:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">james</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">last</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">kirk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$mapped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">map</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">ucfirst</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['first' => 'James', 'last' => 'Kirk']</span></div></code></pre> <h4 id="method-array-map-spread" class="collection-method"><a href="#method-array-map-spread"><code>Arr::mapSpread()</code></a></h4> <p>The <code>Arr::mapSpread</code> method iterates over the array, passing each nested item value into the given closure. The closure is free to modify the item and return it, thus forming a new array of modified items:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$mapped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">mapSpread</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$even</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$odd</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$even</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$odd</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [1, 5, 9, 13, 17]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-map-with-keys" class="collection-method"><a href="#method-array-map-with-keys"><code>Arr::mapWithKeys()</code></a></h4> <p>The <code>Arr::mapWithKeys</code> method iterates through the array and passes each value to the given callback. The callback should return an associative array containing a single key / value pair:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e242126200e2b362f233e222b602d2123">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> ],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Marketing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a202b242f0a2f322b273a262f64292527">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> ]</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$mapped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">mapWithKeys</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]];</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52383d3a3c12372a333f223e377c313d3f">[email protected]</a>' => 'John',</span></div><div class='line'><span style="color: #697098;"> '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c9c2cdc6e3c6dbc2ced3cfc68dc0ccce">[email protected]</a>' => 'Jane',</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-only" class="collection-method"><a href="#method-array-only"><code>Arr::only()</code></a></h4> <p>The <code>Arr::only</code> method returns only the specified key / value pairs from the given array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orders</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$slice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">only</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Desk', 'price' => 100]</span></div></code></pre> <h4 id="method-array-pluck" class="collection-method"><a href="#method-array-pluck"><code>Arr::pluck()</code></a></h4> <p>The <code>Arr::pluck</code> method retrieves all of the values for a given key from an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$names</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">developer.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Taylor', 'Abigail']</span></div></code></pre> <p>You may also specify how you wish the resulting list to be keyed:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$names</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">developer.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">developer.id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => 'Taylor', 2 => 'Abigail']</span></div></code></pre> <h4 id="method-array-prepend" class="collection-method"><a href="#method-array-prepend"><code>Arr::prepend()</code></a></h4> <p>The <code>Arr::prepend</code> method will push an item onto the beginning of an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">one</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">three</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">four</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">zero</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['zero', 'one', 'two', 'three', 'four']</span></div></code></pre> <p>If needed, you may specify the key that should be used for the value:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Desk', 'price' => 100]</span></div></code></pre> <h4 id="method-array-prependkeyswith" class="collection-method"><a href="#method-array-prependkeyswith"><code>Arr::prependKeysWith()</code></a></h4> <p>The <code>Arr::prependKeysWith</code> prepends all key names of an associative array with the given prefix:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">prependKeysWith</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> 'product.name' => 'Desk',</span></div><div class='line'><span style="color: #697098;"> 'product.price' => 100,</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-pull" class="collection-method"><a href="#method-array-pull"><code>Arr::pull()</code></a></h4> <p>The <code>Arr::pull</code> method returns and removes a key / value pair from an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pull</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> $name: Desk</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> $array: ['price' => 100]</span></div></code></pre> <p>A default value may be passed as the third argument to the method. This value will be returned if the key doesn't exist:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pull</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$key</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-array-query" class="collection-method"><a href="#method-array-query"><code>Arr::query()</code></a></h4> <p>The <code>Arr::query</code> method converts the array into a query string:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">order</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">column</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">created_at</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">direction</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desc</span><span style="color: #D9F5DD;">'</span></div><div class='line'><span style="color: #BFC7D5;"> ]</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> name=Taylor&order[column]=created_at&order[direction]=desc</span></div></code></pre> <h4 id="method-array-random" class="collection-method"><a href="#method-array-random"><code>Arr::random()</code></a></h4> <p>The <code>Arr::random</code> method returns a random value from an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$random</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 4 - (retrieved randomly)</span></div></code></pre> <p>You may also specify the number of items to return as an optional second argument. Note that providing this argument will return an array even if only one item is desired:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$items</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 5] - (retrieved randomly)</span></div></code></pre> <h4 id="method-array-set" class="collection-method"><a href="#method-array-set"><code>Arr::set()</code></a></h4> <p>The <code>Arr::set</code> method sets a value within a deeply nested array using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">set</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => ['price' => 200]]]</span></div></code></pre> <h4 id="method-array-shuffle" class="collection-method"><a href="#method-array-shuffle"><code>Arr::shuffle()</code></a></h4> <p>The <code>Arr::shuffle</code> method randomly shuffles the items in the array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">shuffle</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 2, 5, 1, 4] - (generated randomly)</span></div></code></pre> <h4 id="method-array-sort" class="collection-method"><a href="#method-array-sort"><code>Arr::sort()</code></a></h4> <p>The <code>Arr::sort</code> method sorts an array by its values:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Table</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Chair', 'Desk', 'Table']</span></div></code></pre> <p>You may also sort the array by the results of a given closure:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Table</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">array_values</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">array</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'><span style="color: #BFC7D5;">}));</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Chair'],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk'],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Table'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-sort-desc" class="collection-method"><a href="#method-array-sort-desc"><code>Arr::sortDesc()</code></a></h4> <p>The <code>Arr::sortDesc</code> method sorts an array in descending order by its values:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Table</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sortDesc</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Table', 'Desk', 'Chair']</span></div></code></pre> <p>You may also sort the array by the results of a given closure:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Table</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">array_values</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sortDesc</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">array</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">array</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class='line'><span style="color: #BFC7D5;">}));</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Table'],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk'],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Chair'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-sort-recursive" class="collection-method"><a href="#method-array-sort-recursive"><code>Arr::sortRecursive()</code></a></h4> <p>The <code>Arr::sortRecursive</code> method recursively sorts an array using the <code>sort</code> function for numerically indexed sub-arrays and the <code>ksort</code> function for associative sub-arrays:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Roman</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Li</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">PHP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Ruby</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">JavaScript</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">one</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">three</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sortRecursive</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> ['JavaScript', 'PHP', 'Ruby'],</span></div><div class='line'><span style="color: #697098;"> ['one' => 1, 'three' => 3, 'two' => 2],</span></div><div class='line'><span style="color: #697098;"> ['Li', 'Roman', 'Taylor'],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <p>If you would like the results sorted in descending order, you may use the <code>Arr::sortRecursiveDesc</code> method.</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sortRecursiveDesc</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-array-take" class="collection-method"><a href="#method-array-take"><code>Arr::take()</code></a></h4> <p>The <code>Arr::take</code> method returns a new array with the specified number of items:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">take</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [0, 1, 2]</span></div></code></pre> <p>You may also pass a negative integer to take the specified number of items from the end of the array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">take</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [4, 5]</span></div></code></pre> <h4 id="method-array-to-css-classes" class="collection-method"><a href="#method-array-to-css-classes"><code>Arr::toCssClasses()</code></a></h4> <p>The <code>Arr::toCssClasses</code> method conditionally compiles a CSS class string. The method accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$isActive</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BEC5D4;">$hasError</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">p-4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">font-bold</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$isActive</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">bg-red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$hasError</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$classes</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">toCssClasses</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> 'p-4 bg-red'</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-array-to-css-styles" class="collection-method"><a href="#method-array-to-css-styles"><code>Arr::toCssStyles()</code></a></h4> <p>The <code>Arr::toCssStyles</code> conditionally compiles a CSS style string. The method accepts an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression. If the array element has a numeric key, it will always be included in the rendered class list:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$hasColor</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">background-color: blue</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color: blue</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$hasColor</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$classes</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">toCssStyles</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> 'background-color: blue; color: blue;'</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> </div> <p>This method powers Laravel's functionality allowing <a href="/docs/11.x/blade#conditionally-merge-classes">merging classes with a Blade component's attribute bag</a> as well as the <code>@class</code> <a href="/docs/11.x/blade#conditional-classes">Blade directive</a>.</p> <h4 id="method-array-undot" class="collection-method"><a href="#method-array-undot"><code>Arr::undot()</code></a></h4> <p>The <code>Arr::undot</code> method expands a single-dimensional array that uses "dot" notation into a multi-dimensional array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Kevin Malone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user.occupation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Accountant</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">undot</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]</span></div></code></pre> <h4 id="method-array-where" class="collection-method"><a href="#method-array-where"><code>Arr::where()</code></a></h4> <p>The <code>Arr::where</code> method filters an array using the given closure:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">200</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">400</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;">|</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">is_string</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => '200', 3 => '400']</span></div></code></pre> <h4 id="method-array-where-not-null" class="collection-method"><a href="#method-array-where-not-null"><code>Arr::whereNotNull()</code></a></h4> <p>The <code>Arr::whereNotNull</code> method removes all <code>null</code> values from the given array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">whereNotNull</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [0 => 0]</span></div></code></pre> <h4 id="method-array-wrap" class="collection-method"><a href="#method-array-wrap"><code>Arr::wrap()</code></a></h4> <p>The <code>Arr::wrap</code> method wraps the given value in an array. If the given value is already an array it will be returned without modification:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$string</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Laravel</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$string</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Laravel']</span></div></code></pre> <p>If the given value is <code>null</code>, an empty array will be returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Arr</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Arr</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> []</span></div></code></pre> <h4 id="method-data-fill" class="collection-method"><a href="#method-data-fill"><code>data_fill()</code></a></h4> <p>The <code>data_fill</code> function sets a missing value within a nested array or object using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_fill</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => ['price' => 100]]]</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_fill</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]</span></div></code></pre> <p>This function also accepts asterisks as wildcards and will fill the target accordingly:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> ],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_fill</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.*.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> 'products' => [</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 1', 'price' => 100],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 2', 'price' => 200],</span></div><div class='line'><span style="color: #697098;"> ],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-data-get" class="collection-method"><a href="#method-data-get"><code>data_get()</code></a></h4> <p>The <code>data_get</code> function retrieves a value from a nested array or object using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">data_get</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100</span></div></code></pre> <p>The <code>data_get</code> function also accepts a default value, which will be returned if the specified key is not found:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$discount</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">data_get</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 0</span></div></code></pre> <p>The function also accepts wildcards using asterisks, which may target any key of the array or object:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product-one</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product-two</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_get</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">*.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['Desk 1', 'Desk 2'];</span></div></code></pre> <p>The <code>{first}</code> and <code>{last}</code> placeholders may be used to retrieve the first or last items in an array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$flight</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">segments</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">from</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">LHR</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">departure</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">9:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">to</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">IST</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">arrival</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">15:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">from</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">IST</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">departure</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">16:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">to</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">PKX</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">arrival</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">20:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> ],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_get</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">flight</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">segments.{first}.arrival</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 15:00</span></div></code></pre> <h4 id="method-data-set" class="collection-method"><a href="#method-data-set"><code>data_set()</code></a></h4> <p>The <code>data_set</code> function sets a value within a nested array or object using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_set</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => ['price' => 200]]]</span></div></code></pre> <p>This function also accepts wildcards using asterisks and will set values on the target accordingly:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> ],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_set</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.*.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> 'products' => [</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 1', 'price' => 200],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 2', 'price' => 200],</span></div><div class='line'><span style="color: #697098;"> ],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <p>By default, any existing values are overwritten. If you wish to only set a value if it doesn't exist, you may pass <code>false</code> as the fourth argument to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_set</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> overwrite</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => ['price' => 100]]]</span></div></code></pre> <h4 id="method-data-forget" class="collection-method"><a href="#method-data-forget"><code>data_forget()</code></a></h4> <p>The <code>data_forget</code> function removes a value within a nested array or object using "dot" notation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">]]];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_forget</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.desk.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ['products' => ['desk' => []]]</span></div></code></pre> <p>This function also accepts wildcards using asterisks and will remove values on the target accordingly:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> [</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Desk 2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">150</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;"> ],</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">data_forget</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">products.*.price</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/*</span></div><div class='line'><span style="color: #697098;"> [</span></div><div class='line'><span style="color: #697098;"> 'products' => [</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 1'],</span></div><div class='line'><span style="color: #697098;"> ['name' => 'Desk 2'],</span></div><div class='line'><span style="color: #697098;"> ],</span></div><div class='line'><span style="color: #697098;"> ]</span></div><div class='line'><span style="color: #697098;">*/</span></div></code></pre> <h4 id="method-head" class="collection-method"><a href="#method-head"><code>head()</code></a></h4> <p>The <code>head</code> function returns the first element in the given array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$first</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">head</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100</span></div></code></pre> <h4 id="method-last" class="collection-method"><a href="#method-last"><code>last()</code></a></h4> <p>The <code>last</code> function returns the last element in the given array:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$array</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">];</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$last</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">array</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 300</span></div></code></pre> <h2 id="numbers"><a href="#numbers">Numbers</a></h2> <h4 id="method-number-abbreviate" class="collection-method"><a href="#method-number-abbreviate"><code>Number::abbreviate()</code></a></h4> <p>The <code>Number::abbreviate</code> method returns the human-readable format of the provided numerical value, with an abbreviation for the units:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">abbreviate</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1K</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">abbreviate</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">489939</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 490K</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">abbreviate</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1230000</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1.23M</span></div></code></pre> <h4 id="method-number-clamp" class="collection-method"><a href="#method-number-clamp"><code>Number::clamp()</code></a></h4> <p>The <code>Number::clamp</code> method ensures a given number stays within a specified range. If the number is lower than the minimum, the minimum value is returned. If the number is higher than the maximum, the maximum value is returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">clamp</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">105</span><span style="color: #BFC7D5;">, min: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, max: </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">clamp</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, min: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, max: </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">clamp</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, min: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, max: </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">clamp</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">, min: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, max: </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 20</span></div></code></pre> <h4 id="method-number-currency" class="collection-method"><a href="#method-number-currency"><code>Number::currency()</code></a></h4> <p>The <code>Number::currency</code> method returns the currency representation of the given value as a string:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$currency</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">currency</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> $1,000.00</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$currency</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">currency</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">, in: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">EUR</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> €1,000.00</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$currency</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">currency</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">, in: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">EUR</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, locale: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">de</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1.000,00 €</span></div></code></pre> <h4 id="method-default-currency" class="collection-method"><a href="#method-default-currency"><code>Number::defaultCurrency()</code></a></h4> <p>The <code>Number::defaultCurrency</code> method returns the default currency being used by the <code>Number</code> class:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$currency</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">defaultCurrency</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> USD</span></div></code></pre> <h4 id="method-default-locale" class="collection-method"><a href="#method-default-locale"><code>Number::defaultLocale()</code></a></h4> <p>The <code>Number::defaultLocale</code> method returns the default locale being used by the <code>Number</code> class:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$locale</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">defaultLocale</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> en</span></div></code></pre> <h4 id="method-number-file-size" class="collection-method"><a href="#method-number-file-size"><code>Number::fileSize()</code></a></h4> <p>The <code>Number::fileSize</code> method returns the file size representation of the given byte value as a string:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$size</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fileSize</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1024</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1 KB</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$size</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fileSize</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1024</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1024</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1 MB</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$size</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fileSize</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1024</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1.00 KB</span></div></code></pre> <h4 id="method-number-for-humans" class="collection-method"><a href="#method-number-for-humans"><code>Number::forHumans()</code></a></h4> <p>The <code>Number::forHumans</code> method returns the human-readable format of the provided numerical value:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forHumans</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1 thousand</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forHumans</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">489939</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 490 thousand</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forHumans</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1230000</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1.23 million</span></div></code></pre> <h4 id="method-number-format" class="collection-method"><a href="#method-number-format"><code>Number::format()</code></a></h4> <p>The <code>Number::format</code> method formats the given number into a locale specific string:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100000</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100,000</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100000</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100,000.00</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100000.123</span><span style="color: #BFC7D5;">, maxPrecision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100,000.12</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100000</span><span style="color: #BFC7D5;">, locale: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">de</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 100.000</span></div></code></pre> <h4 id="method-number-ordinal" class="collection-method"><a href="#method-number-ordinal"><code>Number::ordinal()</code></a></h4> <p>The <code>Number::ordinal</code> method returns a number's ordinal representation:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">ordinal</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 1st</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">ordinal</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 2nd</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">ordinal</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">21</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 21st</span></div></code></pre> <h4 id="method-number-pairs" class="collection-method"><a href="#method-number-pairs"><code>Number::pairs()</code></a></h4> <p>The <code>Number::pairs</code> method generates an array of number pairs (sub-ranges) based on a specified range and step value. This method can be useful for dividing a larger range of numbers into smaller, manageable sub-ranges for things like pagination or batching tasks. The <code>pairs</code> method returns an array of arrays, where each inner array represents a pair (sub-range) of numbers:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pairs</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">25</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [[1, 10], [11, 20], [21, 25]]</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pairs</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">25</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, offset: </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> [[0, 10], [10, 20], [20, 25]]</span></div></code></pre> </div> <h4 id="method-number-percentage" class="collection-method"><a href="#method-number-percentage"><code>Number::percentage()</code></a></h4> <p>The <code>Number::percentage</code> method returns the percentage representation of the given value as a string:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10%</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10.00%</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10.123</span><span style="color: #BFC7D5;">, maxPrecision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10.12%</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$percentage</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">percentage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, precision: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, locale: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">de</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10,00%</span></div></code></pre> <h4 id="method-number-spell" class="collection-method"><a href="#method-number-spell"><code>Number::spell()</code></a></h4> <p>The <code>Number::spell</code> method transforms the given number into a string of words:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">102</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> one hundred and two</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">88</span><span style="color: #BFC7D5;">, locale: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fr</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> quatre-vingt-huit</span></div></code></pre> <p>The <code>after</code> argument allows you to specify a value after which all numbers should be spelled out:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, after: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">11</span><span style="color: #BFC7D5;">, after: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> eleven</span></div></code></pre> <p>The <code>until</code> argument allows you to specify a value before which all numbers should be spelled out:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, until: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> five</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">spell</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, until: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div></code></pre> <h4 id="method-number-trim" class="collection-method"><a href="#method-number-trim"><code>Number::trim()</code></a></h4> <p>The <code>Number::trim</code> method removes any trailing zero digits after the decimal point of the given number:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">trim</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">12.0</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 12</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">trim</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">12.30</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 12.3</span></div></code></pre> <h4 id="method-number-use-locale" class="collection-method"><a href="#method-number-use-locale"><code>Number::useLocale()</code></a></h4> <p>The <code>Number::useLocale</code> method sets the default number locale globally, which affects how numbers and currency are formatted by subsequent invocations to the <code>Number</code> class's methods:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/**</span></div><div class='line'><span style="color: #697098;"> * Bootstrap any application services.</span></div><div class='line'><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class='line'><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">boot</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">void</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useLocale</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">de</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> <h4 id="method-number-with-locale" class="collection-method"><a href="#method-number-with-locale"><code>Number::withLocale()</code></a></h4> <p>The <code>Number::withLocale</code> method executes the given closure using the specified locale and then restores the original locale after the callback has executed:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">withLocale</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">de</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1500</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h4 id="method-number-use-currency" class="collection-method"><a href="#method-number-use-currency"><code>Number::useCurrency()</code></a></h4> <p>The <code>Number::useCurrency</code> method sets the default number currency globally, which affects how the currency is formatted by subsequent invocations to the <code>Number</code> class's methods:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">/**</span></div><div class='line'><span style="color: #697098;"> * Bootstrap any application services.</span></div><div class='line'><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class='line'><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">boot</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">void</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useCurrency</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">GBP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> <h4 id="method-number-with-currency" class="collection-method"><a href="#method-number-with-currency"><code>Number::withCurrency()</code></a></h4> <p>The <code>Number::withCurrency</code> method executes the given closure using the specified currency and then restores the original currency after the callback has executed:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Number</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Number</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">withCurrency</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">GBP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h2 id="paths"><a href="#paths">Paths</a></h2> <h4 id="method-app-path" class="collection-method"><a href="#method-app-path"><code>app_path()</code></a></h4> <p>The <code>app_path</code> function returns the fully qualified path to your application's <code>app</code> directory. You may also use the <code>app_path</code> function to generate a fully qualified path to a file relative to the application directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">app_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">app_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Http/Controllers/Controller.php</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-base-path" class="collection-method"><a href="#method-base-path"><code>base_path()</code></a></h4> <p>The <code>base_path</code> function returns the fully qualified path to your application's root directory. You may also use the <code>base_path</code> function to generate a fully qualified path to a given file relative to the project root directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">base_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">base_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">vendor/bin</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-config-path" class="collection-method"><a href="#method-config-path"><code>config_path()</code></a></h4> <p>The <code>config_path</code> function returns the fully qualified path to your application's <code>config</code> directory. You may also use the <code>config_path</code> function to generate a fully qualified path to a given file within the application's configuration directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">config_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">config_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">app.php</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-database-path" class="collection-method"><a href="#method-database-path"><code>database_path()</code></a></h4> <p>The <code>database_path</code> function returns the fully qualified path to your application's <code>database</code> directory. You may also use the <code>database_path</code> function to generate a fully qualified path to a given file within the database directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">database_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">database_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">factories/UserFactory.php</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-lang-path" class="collection-method"><a href="#method-lang-path"><code>lang_path()</code></a></h4> <p>The <code>lang_path</code> function returns the fully qualified path to your application's <code>lang</code> directory. You may also use the <code>lang_path</code> function to generate a fully qualified path to a given file within the directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">lang_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">lang_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">en/messages.php</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <div class="mb-10 max-w-2xl mx-auto px-4 py-8 shadow-lg lg:flex lg:items-center callout"> <div class="w-20 h-20 mb-6 flex items-center justify-center shrink-0 lg:mb-0 bg-purple-600"> <div class="opacity-75"><svg width="28" height="40" viewBox="0 0 28 40" xmlns="http://www.w3.org/2000/svg"><title>lightbulb</title><path d="M12 28h4v-8h-4v8zM8 40h12v-8H8v8zm13.98-14.52c-1.001.705-1.661 1.545-1.98 2.52H8c-.416-.959-1.076-1.799-1.98-2.52A13.99 13.99 0 0 1 0 14C0 6.272 6.272 0 14 0s14 6.272 14 14a13.99 13.99 0 0 1-6.02 11.48z" fill="#FFF" fill-rule="nonzero"/></svg></div> </div> <p class="mb-0 lg:ml-6 callout"> By default, the Laravel application skeleton does not include the <code>lang</code> directory. If you would like to customize Laravel's language files, you may publish them via the <code>lang:publish</code> Artisan command.</p> </div> <h4 id="method-mix" class="collection-method"><a href="#method-mix"><code>mix()</code></a></h4> <p>The <code>mix</code> function returns the path to a <a href="/docs/11.x/mix">versioned Mix file</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">mix</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">css/app.css</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-public-path" class="collection-method"><a href="#method-public-path"><code>public_path()</code></a></h4> <p>The <code>public_path</code> function returns the fully qualified path to your application's <code>public</code> directory. You may also use the <code>public_path</code> function to generate a fully qualified path to a given file within the public directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">public_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">public_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">css/app.css</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-resource-path" class="collection-method"><a href="#method-resource-path"><code>resource_path()</code></a></h4> <p>The <code>resource_path</code> function returns the fully qualified path to your application's <code>resources</code> directory. You may also use the <code>resource_path</code> function to generate a fully qualified path to a given file within the resources directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">resource_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">resource_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">sass/app.scss</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-storage-path" class="collection-method"><a href="#method-storage-path"><code>storage_path()</code></a></h4> <p>The <code>storage_path</code> function returns the fully qualified path to your application's <code>storage</code> directory. You may also use the <code>storage_path</code> function to generate a fully qualified path to a given file within the storage directory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">storage_path</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$path</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">storage_path</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">app/file.txt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h2 id="urls"><a href="#urls">URLs</a></h2> <h4 id="method-action" class="collection-method"><a href="#method-action"><code>action()</code></a></h4> <p>The <code>action</code> function generates a URL for the given controller action:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Http\Controllers\</span><span style="color: #FFCB8B;">HomeController</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">action</span><span style="color: #BFC7D5;">([</span><span style="color: #FFCB8B;">HomeController</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">index</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>If the method accepts route parameters, you may pass them as the second argument to the method:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">action</span><span style="color: #BFC7D5;">([</span><span style="color: #FFCB8B;">UserController</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">profile</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-asset" class="collection-method"><a href="#method-asset"><code>asset()</code></a></h4> <p>The <code>asset</code> function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS):</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">asset</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">img/photo.jpg</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You can configure the asset URL host by setting the <code>ASSET_URL</code> variable in your <code>.env</code> file. This can be useful if you host your assets on an external service like Amazon S3 or another CDN:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> ASSET_URL=http://example.com/assets</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">asset</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">img/photo.jpg</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> http://example.com/assets/img/photo.jpg</span></div></code></pre> <h4 id="method-route" class="collection-method"><a href="#method-route"><code>route()</code></a></h4> <p>The <code>route</code> function generates a URL for a given <a href="/docs/11.x/routing#named-routes">named route</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">route.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>If the route accepts parameters, you may pass them as the second argument to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">route.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>By default, the <code>route</code> function generates an absolute URL. If you wish to generate a relative URL, you may pass <code>false</code> as the third argument to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">route.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-secure-asset" class="collection-method"><a href="#method-secure-asset"><code>secure_asset()</code></a></h4> <p>The <code>secure_asset</code> function generates a URL for an asset using HTTPS:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">secure_asset</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">img/photo.jpg</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-secure-url" class="collection-method"><a href="#method-secure-url"><code>secure_url()</code></a></h4> <p>The <code>secure_url</code> function generates a fully qualified HTTPS URL to the given path. Additional URL segments may be passed in the function's second argument:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">secure_url</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user/profile</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">secure_url</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user/profile</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-to-route" class="collection-method"><a href="#method-to-route"><code>to_route()</code></a></h4> <p>The <code>to_route</code> function generates a <a href="/docs/11.x/responses#redirects">redirect HTTP response</a> for a given <a href="/docs/11.x/routing#named-routes">named route</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">to_route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users.show</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>If necessary, you may pass the HTTP status code that should be assigned to the redirect and any additional response headers as the third and fourth arguments to the <code>to_route</code> method:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">to_route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users.show</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">302</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">X-Framework</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Laravel</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-url" class="collection-method"><a href="#method-url"><code>url()</code></a></h4> <p>The <code>url</code> function generates a fully qualified URL to the given path:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">url</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user/profile</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$url</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">url</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user/profile</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>If no path is provided, an <code>Illuminate\Routing\UrlGenerator</code> instance is returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$current</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">url</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">current</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$full</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">url</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">full</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$previous</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">url</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">previous</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h2 id="miscellaneous"><a href="#miscellaneous">Miscellaneous</a></h2> <h4 id="method-abort" class="collection-method"><a href="#method-abort"><code>abort()</code></a></h4> <p>The <code>abort</code> function throws <a href="/docs/11.x/errors#http-exceptions">an HTTP exception</a> which will be rendered by the <a href="/docs/11.x/errors#handling-exceptions">exception handler</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">abort</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">403</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You may also provide the exception's message and custom HTTP response headers that should be sent to the browser:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">abort</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">403</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Unauthorized.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">headers</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-abort-if" class="collection-method"><a href="#method-abort-if"><code>abort_if()</code></a></h4> <p>The <code>abort_if</code> function throws an HTTP exception if a given boolean expression evaluates to <code>true</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">abort_if</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">!</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">403</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>Like the <code>abort</code> method, you may also provide the exception's response text as the third argument and an array of custom response headers as the fourth argument to the function.</p> <h4 id="method-abort-unless" class="collection-method"><a href="#method-abort-unless"><code>abort_unless()</code></a></h4> <p>The <code>abort_unless</code> function throws an HTTP exception if a given boolean expression evaluates to <code>false</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">abort_unless</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">403</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>Like the <code>abort</code> method, you may also provide the exception's response text as the third argument and an array of custom response headers as the fourth argument to the function.</p> <h4 id="method-app" class="collection-method"><a href="#method-app"><code>app()</code></a></h4> <p>The <code>app</code> function returns the <a href="/docs/11.x/container">service container</a> instance:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$container</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">app</span><span style="color: #BFC7D5;">();</span></div></code></pre> <p>You may pass a class or interface name to resolve it from the container:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$api</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">app</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">HelpSpot\API</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-auth" class="collection-method"><a href="#method-auth"><code>auth()</code></a></h4> <p>The <code>auth</code> function returns an <a href="/docs/11.x/authentication">authenticator</a> instance. You may use it as an alternative to the <code>Auth</code> facade:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">auth</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">();</span></div></code></pre> <p>If needed, you may specify which guard instance you would like to access:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">auth</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">admin</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-back" class="collection-method"><a href="#method-back"><code>back()</code></a></h4> <p>The <code>back</code> function generates a <a href="/docs/11.x/responses#redirects">redirect HTTP response</a> to the user's previous location:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">back</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">status</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">302</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">headers</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[],</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">fallback</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">back</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-bcrypt" class="collection-method"><a href="#method-bcrypt"><code>bcrypt()</code></a></h4> <p>The <code>bcrypt</code> function <a href="/docs/11.x/hashing">hashes</a> the given value using Bcrypt. You may use this function as an alternative to the <code>Hash</code> facade:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$password</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">bcrypt</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">my-secret-password</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-blank" class="collection-method"><a href="#method-blank"><code>blank()</code></a></h4> <p>The <code>blank</code> function determines whether the given value is "blank":</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">''</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">());</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">blank</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <p>For the inverse of <code>blank</code>, see the <a href="#method-filled"><code>filled</code></a> method.</p> <h4 id="method-broadcast" class="collection-method"><a href="#method-broadcast"><code>broadcast()</code></a></h4> <p>The <code>broadcast</code> function <a href="/docs/11.x/broadcasting">broadcasts</a> the given <a href="/docs/11.x/events">event</a> to its listeners:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">broadcast</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">UserRegistered</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">));</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">broadcast</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">UserRegistered</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">))</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toOthers</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-cache" class="collection-method"><a href="#method-cache"><code>cache()</code></a></h4> <p>The <code>cache</code> function may be used to get values from the <a href="/docs/11.x/cache">cache</a>. If the given key does not exist in the cache, an optional default value will be returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cache</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cache</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You may add items to the cache by passing an array of key / value pairs to the function. You should also pass the number of seconds or duration the cached value should be considered valid:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">cache</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">300</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">cache</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addSeconds</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">));</span></div></code></pre> <h4 id="method-class-uses-recursive" class="collection-method"><a href="#method-class-uses-recursive"><code>class_uses_recursive()</code></a></h4> <p>The <code>class_uses_recursive</code> function returns all traits used by a class, including traits used by all of its parent classes:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$traits</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">class_uses_recursive</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">App</span><span style="color: #BFC7D5;">\</span><span style="color: #82AAFF;">Models</span><span style="color: #BFC7D5;">\</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-collect" class="collection-method"><a href="#method-collect"><code>collect()</code></a></h4> <p>The <code>collect</code> function creates a <a href="/docs/11.x/collections">collection</a> instance from the given value:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">abigail</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-config" class="collection-method"><a href="#method-config"><code>config()</code></a></h4> <p>The <code>config</code> function gets the value of a <a href="/docs/11.x/configuration">configuration</a> variable. The configuration values may be accessed using "dot" syntax, which includes the name of the file and the option you wish to access. A default value may be specified and is returned if the configuration option does not exist:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">config</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">app.timezone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">config</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">app.timezone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You may set configuration variables at runtime by passing an array of key / value pairs. However, note that this function only affects the configuration value for the current request and does not update your actual configuration values:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">config</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">app.debug</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> true</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-context" class="collection-method"><a href="#method-context"><code>context()</code></a></h4> <p>The <code>context</code> function gets the value from the <a href="/docs/11.x/context">current context</a>. A default value may be specified and is returned if the context key does not exist:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">context</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">trace_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">context</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">trace_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You may set context values by passing an array of key / value pairs:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Str</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">context</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">trace_id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Str</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">uuid</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toString</span><span style="color: #BFC7D5;">()]);</span></div></code></pre> <h4 id="method-cookie" class="collection-method"><a href="#method-cookie"><code>cookie()</code></a></h4> <p>The <code>cookie</code> function creates a new <a href="/docs/11.x/requests#cookies">cookie</a> instance:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$cookie</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cookie</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">minutes</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-csrf-field" class="collection-method"><a href="#method-csrf-field"><code>csrf_field()</code></a></h4> <p>The <code>csrf_field</code> function generates an HTML <code>hidden</code> input field containing the value of the CSRF token. For example, using <a href="/docs/11.x/blade">Blade syntax</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BFC7D5;">{{ </span><span style="color: #82AAFF;">csrf_field</span><span style="color: #BFC7D5;">() }}</span></div></code></pre> <h4 id="method-csrf-token" class="collection-method"><a href="#method-csrf-token"><code>csrf_token()</code></a></h4> <p>The <code>csrf_token</code> function retrieves the value of the current CSRF token:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$token</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">csrf_token</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-decrypt" class="collection-method"><a href="#method-decrypt"><code>decrypt()</code></a></h4> <p>The <code>decrypt</code> function <a href="/docs/11.x/encryption">decrypts</a> the given value. You may use this function as an alternative to the <code>Crypt</code> facade:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$password</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">decrypt</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-dd" class="collection-method"><a href="#method-dd"><code>dd()</code></a></h4> <p>The <code>dd</code> function dumps the given variables and ends the execution of the script:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">...</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>If you do not want to halt the execution of your script, use the <a href="#method-dump"><code>dump</code></a> function instead.</p> <h4 id="method-dispatch" class="collection-method"><a href="#method-dispatch"><code>dispatch()</code></a></h4> <p>The <code>dispatch</code> function pushes the given <a href="/docs/11.x/queues#creating-jobs">job</a> onto the Laravel <a href="/docs/11.x/queues">job queue</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">dispatch</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> App</span><span style="color: #BFC7D5;">\</span><span style="color: #82AAFF;">Jobs</span><span style="color: #BFC7D5;">\</span><span style="color: #FFCB8B;">SendEmails</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-dispatch-sync" class="collection-method"><a href="#method-dispatch-sync"><code>dispatch_sync()</code></a></h4> <p>The <code>dispatch_sync</code> function pushes the given job to the <a href="/docs/11.x/queues#synchronous-dispatching">sync</a> queue so that it is processed immediately:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">dispatch_sync</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> App</span><span style="color: #BFC7D5;">\</span><span style="color: #82AAFF;">Jobs</span><span style="color: #BFC7D5;">\</span><span style="color: #FFCB8B;">SendEmails</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-dump" class="collection-method"><a href="#method-dump"><code>dump()</code></a></h4> <p>The <code>dump</code> function dumps the given variables:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">value3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">...</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>If you want to stop executing the script after dumping the variables, use the <a href="#method-dd"><code>dd</code></a> function instead.</p> <h4 id="method-encrypt" class="collection-method"><a href="#method-encrypt"><code>encrypt()</code></a></h4> <p>The <code>encrypt</code> function <a href="/docs/11.x/encryption">encrypts</a> the given value. You may use this function as an alternative to the <code>Crypt</code> facade:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$secret</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">encrypt</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">my-secret-value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-env" class="collection-method"><a href="#method-env"><code>env()</code></a></h4> <p>The <code>env</code> function retrieves the value of an <a href="/docs/11.x/configuration#environment-configuration">environment variable</a> or returns a default value:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$env</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">env</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">APP_ENV</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$env</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">env</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">APP_ENV</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">production</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <div class="mb-10 max-w-2xl mx-auto px-4 py-8 shadow-lg lg:flex lg:items-center callout"> <div class="w-20 h-20 mb-6 flex items-center justify-center shrink-0 lg:mb-0 bg-red-600"> <div class="opacity-75"><svg width="6" height="35" viewBox="0 0 6 35" xmlns="http://www.w3.org/2000/svg"><title>exclamation</title><path d="M0 29h6v6H0v-6zM0 0h6v24H0V0z" fill="#FFF" fill-rule="nonzero"/></svg> </div> </div> <p class="mb-0 lg:ml-6 callout"> If you execute the <code>config:cache</code> command during your deployment process, you should be sure that you are only calling the <code>env</code> function from within your configuration files. Once the configuration has been cached, the <code>.env</code> file will not be loaded and all calls to the <code>env</code> function will return <code>null</code>.</p> </div> <h4 id="method-event" class="collection-method"><a href="#method-event"><code>event()</code></a></h4> <p>The <code>event</code> function dispatches the given <a href="/docs/11.x/events">event</a> to its listeners:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">event</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">UserRegistered</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">));</span></div></code></pre> <h4 id="method-fake" class="collection-method"><a href="#method-fake"><code>fake()</code></a></h4> <p>The <code>fake</code> function resolves a <a href="https://github.com/FakerPHP/Faker">Faker</a> singleton from the container, which can be useful when creating fake data in model factories, database seeding, tests, and prototyping views:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="blade" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">@for</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$i</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">; </span><span style="color: #BEC5D4;">$i</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">; </span><span style="color: #BEC5D4;">$i</span><span style="color: #89DDFF;">++</span><span style="color: #BFC7D5;">)</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">dl</span><span style="color: #89DDFF;">></span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">dt</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Name</span><span style="color: #89DDFF;"></</span><span style="color: #FF5572;">dt</span><span style="color: #89DDFF;">></span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">dd</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">name</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</</span><span style="color: #FF5572;">dd</span><span style="color: #89DDFF;">></span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">dt</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Email</span><span style="color: #89DDFF;"></</span><span style="color: #FF5572;">dt</span><span style="color: #89DDFF;">></span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">dd</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">safeEmail</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</</span><span style="color: #FF5572;">dd</span><span style="color: #89DDFF;">></span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></</span><span style="color: #FF5572;">dl</span><span style="color: #89DDFF;">></span></div><div class='line'><span style="color: #C792EA;">@endfor</span></div></code></pre> </div> <p>By default, the <code>fake</code> function will utilize the <code>app.faker_locale</code> configuration option in your <code>config/app.php</code> configuration. Typically, this configuration option is set via the <code>APP_FAKER_LOCALE</code> environment variable. You may also specify the locale by passing it to the <code>fake</code> function. Each locale will resolve an individual singleton:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">nl_NL</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">name</span><span style="color: #BFC7D5;">()</span></div></code></pre> <h4 id="method-filled" class="collection-method"><a href="#method-filled"><code>filled()</code></a></h4> <p>The <code>filled</code> function determines whether the given value is not "blank":</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">''</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;">filled</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">());</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <p>For the inverse of <code>filled</code>, see the <a href="#method-blank"><code>blank</code></a> method.</p> <h4 id="method-info" class="collection-method"><a href="#method-info"><code>info()</code></a></h4> <p>The <code>info</code> function will write information to your application's <a href="/docs/11.x/logging">log</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">info</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Some helpful information!</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>An array of contextual data may also be passed to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">info</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User login attempt failed.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <h4 id="method-literal" class="collection-method"><a href="#method-literal"><code>literal()</code></a></h4> <p>The <code>literal</code> function creates a new <a href="https://www.php.net/manual/en/class.stdclass.php">stdClass</a> instance with the given named arguments as properties:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$obj</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">literal</span><span style="color: #BFC7D5;">(</span></div><div class='line'><span style="color: #82AAFF;"> name</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Joe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #82AAFF;"> languages</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">PHP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Ruby</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class='line'><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$obj</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;">; </span><span style="color: #697098;">//</span><span style="color: #697098;"> 'Joe'</span></div><div class='line'><span style="color: #BEC5D4;">$obj</span><span style="color: #89DDFF;">->languages</span><span style="color: #BFC7D5;">; </span><span style="color: #697098;">//</span><span style="color: #697098;"> ['PHP', 'Ruby']</span></div></code></pre> <h4 id="method-logger" class="collection-method"><a href="#method-logger"><code>logger()</code></a></h4> <p>The <code>logger</code> function can be used to write a <code>debug</code> level message to the <a href="/docs/11.x/logging">log</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">logger</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Debug message</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>An array of contextual data may also be passed to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">logger</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User has logged in.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>A <a href="/docs/11.x/logging">logger</a> instance will be returned if no value is passed to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">logger</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">error</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">You are not allowed here.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-method-field" class="collection-method"><a href="#method-method-field"><code>method_field()</code></a></h4> <p>The <code>method_field</code> function generates an HTML <code>hidden</code> input field containing the spoofed value of the form's HTTP verb. For example, using <a href="/docs/11.x/blade">Blade syntax</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">form</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">method</span><span style="color: #C792EA;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">POST</span><span style="color: #D9F5DD;">"</span><span style="color: #C792EA;">></span></div><div class='line'><span style="color: #BFC7D5;"> {{ </span><span style="color: #82AAFF;">method_field</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C792EA;">DELETE</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">) }}</span></div><div class='line'><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">form</span><span style="color: #C792EA;">></span></div></code></pre> <h4 id="method-now" class="collection-method"><a href="#method-now"><code>now()</code></a></h4> <p>The <code>now</code> function creates a new <code>Illuminate\Support\Carbon</code> instance for the current time:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$now</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-old" class="collection-method"><a href="#method-old"><code>old()</code></a></h4> <p>The <code>old</code> function <a href="/docs/11.x/requests#retrieving-input">retrieves</a> an <a href="/docs/11.x/requests#old-input">old input</a> value flashed into the session:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>Since the "default value" provided as the second argument to the <code>old</code> function is often an attribute of an Eloquent model, Laravel allows you to simply pass the entire Eloquent model as the second argument to the <code>old</code> function. When doing so, Laravel will assume the first argument provided to the <code>old</code> function is the name of the Eloquent attribute that should be considered the "default value":</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BFC7D5;">{{ </span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;">) }}</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Is equivalent to...</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;">{{ </span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">) }}</span></div></code></pre> <h4 id="method-once" class="collection-method"><a href="#method-once"><code>once()</code></a></h4> <p>The <code>once</code> function executes the given callback and caches the result in memory for the duration of the request. Any subsequent calls to the <code>once</code> function with the same callback will return the previously cached result:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">random</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">int</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">once</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> random_int</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">});</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 123</span></div><div class='line'><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 123 (cached result)</span></div><div class='line'><span style="color: #82AAFF;">random</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 123 (cached result)</span></div></code></pre> <p>When the <code>once</code> function is executed from within an object instance, the cached result will be unique to that object instance:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #D3423E;"><?php</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">NumberService</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">all</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">array</span></div><div class='line'><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">once</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div><div class='line'><span style="color: #BFC7D5;"> }</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$service</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">NumberService</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$service</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BEC5D4;">$service</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> (cached result)</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$secondService</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">NumberService</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$secondService</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BEC5D4;">$secondService</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> (cached result)</span></div></code></pre> </div> <h4 id="method-optional" class="collection-method"><a href="#method-optional"><code>optional()</code></a></h4> <p>The <code>optional</code> function accepts any argument and allows you to access properties or call methods on that object. If the given object is <code>null</code>, properties and methods will return <code>null</code> instead of causing an error:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">optional</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->address</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">->street</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;">{</span><span style="color: #C792EA;">!!</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> optional</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;">) </span><span style="color: #C792EA;">!!</span><span style="color: #BFC7D5;">}</span></div></code></pre> <p>The <code>optional</code> function also accepts a closure as its second argument. The closure will be invoked if the value provided as the first argument is not null:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">optional</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">find</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">id</span><span style="color: #BFC7D5;">),</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h4 id="method-policy" class="collection-method"><a href="#method-policy"><code>policy()</code></a></h4> <p>The <code>policy</code> method retrieves a <a href="/docs/11.x/authorization#creating-policies">policy</a> instance for a given class:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$policy</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">policy</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">App</span><span style="color: #BFC7D5;">\</span><span style="color: #82AAFF;">Models</span><span style="color: #BFC7D5;">\</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-redirect" class="collection-method"><a href="#method-redirect"><code>redirect()</code></a></h4> <p>The <code>redirect</code> function returns a <a href="/docs/11.x/responses#redirects">redirect HTTP response</a>, or returns the redirector instance if called with no arguments:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">redirect</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">to</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">status</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">302</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">headers</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[],</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">https</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">redirect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/home</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">redirect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">route</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">route.name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-report" class="collection-method"><a href="#method-report"><code>report()</code></a></h4> <p>The <code>report</code> function will report an exception using your <a href="/docs/11.x/errors#handling-exceptions">exception handler</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">report</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">e</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>The <code>report</code> function also accepts a string as an argument. When a string is given to the function, the function will create an exception with the given string as its message:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">report</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Something went wrong.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-report-if" class="collection-method"><a href="#method-report-if"><code>report_if()</code></a></h4> <p>The <code>report_if</code> function will report an exception using your <a href="/docs/11.x/errors#handling-exceptions">exception handler</a> if the given condition is <code>true</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">report_if</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">shouldReport</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">e</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">report_if</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">shouldReport</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Something went wrong.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-report-unless" class="collection-method"><a href="#method-report-unless"><code>report_unless()</code></a></h4> <p>The <code>report_unless</code> function will report an exception using your <a href="/docs/11.x/errors#handling-exceptions">exception handler</a> if the given condition is <code>false</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">report_unless</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">reportingDisabled</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">e</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">report_unless</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">reportingDisabled</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Something went wrong.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-request" class="collection-method"><a href="#method-request"><code>request()</code></a></h4> <p>The <code>request</code> function returns the current <a href="/docs/11.x/requests">request</a> instance or obtains an input field's value from the current request:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$request</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">request</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">request</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">default</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-rescue" class="collection-method"><a href="#method-rescue"><code>rescue()</code></a></h4> <p>The <code>rescue</code> function executes the given closure and catches any exceptions that occur during its execution. All exceptions that are caught will be sent to your <a href="/docs/11.x/errors#handling-exceptions">exception handler</a>; however, the request will continue processing:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">rescue</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">method</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <p>You may also pass a second argument to the <code>rescue</code> function. This argument will be the "default" value that should be returned if an exception occurs while executing the closure:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">rescue</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">method</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">rescue</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">method</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">failure</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <p>A <code>report</code> argument may be provided to the <code>rescue</code> function to determine if the exception should be reported via the <code>report</code> function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">rescue</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">method</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> report</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Throwable</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">throwable</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">throwable</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">instanceof</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB6B;">InvalidArgumentException</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h4 id="method-resolve" class="collection-method"><a href="#method-resolve"><code>resolve()</code></a></h4> <p>The <code>resolve</code> function resolves a given class or interface name to an instance using the <a href="/docs/11.x/container">service container</a>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$api</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">resolve</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">HelpSpot\API</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-response" class="collection-method"><a href="#method-response"><code>response()</code></a></h4> <p>The <code>response</code> function creates a <a href="/docs/11.x/responses">response</a> instance or obtains an instance of the response factory:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">response</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Hello World</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">headers</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">response</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">json</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">bar</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$headers</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-retry" class="collection-method"><a href="#method-retry"><code>retry()</code></a></h4> <p>The <code>retry</code> function attempts to execute the given callback until the given maximum attempt threshold is met. If the callback does not throw an exception, its return value will be returned. If the callback throws an exception, it will automatically be retried. If the maximum attempt count is exceeded, the exception will be thrown:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">retry</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> Attempt 5 times while resting 100ms between attempts...</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>If you would like to manually calculate the number of milliseconds to sleep between attempts, you may pass a closure as the third argument to the <code>retry</code> function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Exception</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">retry</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">attempt</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB6B;">Exception</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">exception</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">attempt</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">*</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <p>For convenience, you may provide an array as the first argument to the <code>retry</code> function. This array will be used to determine how many milliseconds to sleep between subsequent attempts:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">retry</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> Sleep for 100ms on first retry, 200ms on second retry...</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <p>To only retry under specific conditions, you may pass a closure as the fourth argument to the <code>retry</code> function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Exception</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">retry</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB6B;">Exception</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">exception</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">exception</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">instanceof</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">RetryException</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h4 id="method-session" class="collection-method"><a href="#method-session"><code>session()</code></a></h4> <p>The <code>session</code> function may be used to get or set <a href="/docs/11.x/session">session</a> values:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>You may set values by passing an array of key / value pairs to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">chairs</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">instruments</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>The session store will be returned if no value is passed to the function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">put</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-tap" class="collection-method"><a href="#method-tap"><code>tap()</code></a></h4> <p>The <code>tap</code> function accepts two arguments: an arbitrary <code>$value</code> and a closure. The <code>$value</code> will be passed to the closure and then be returned by the <code>tap</code> function. The return value of the closure is irrelevant:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">tap</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">->name</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">=</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">save</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <p>If no closure is passed to the <code>tap</code> function, you may call any method on the given <code>$value</code>. The return value of the method you call will always be <code>$value</code>, regardless of what the method actually returns in its definition. For example, the Eloquent <code>update</code> method typically returns an integer. However, we can force the method to return the model itself by chaining the <code>update</code> method call through the <code>tap</code> function:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">tap</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">user</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">update</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$email</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>To add a <code>tap</code> method to a class, you may add the <code>Illuminate\Support\Traits\Tappable</code> trait to the class. The <code>tap</code> method of this trait accepts a Closure as its only argument. The object instance itself will be passed to the Closure and then be returned by the <code>tap</code> method:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tap</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> <h4 id="method-throw-if" class="collection-method"><a href="#method-throw-if"><code>throw_if()</code></a></h4> <p>The <code>throw_if</code> function throws the given exception if a given boolean expression evaluates to <code>true</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">throw_if</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">!</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">AuthorizationException</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">throw_if</span><span style="color: #BFC7D5;">(</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">!</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">AuthorizationException</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">You are not allowed to access this page.</span><span style="color: #D9F5DD;">'</span></div><div class='line'><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-throw-unless" class="collection-method"><a href="#method-throw-unless"><code>throw_unless()</code></a></h4> <p>The <code>throw_unless</code> function throws the given exception if a given boolean expression evaluates to <code>false</code>:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">throw_unless</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">AuthorizationException</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">throw_unless</span><span style="color: #BFC7D5;">(</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">AuthorizationException</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">You are not allowed to access this page.</span><span style="color: #D9F5DD;">'</span></div><div class='line'><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-today" class="collection-method"><a href="#method-today"><code>today()</code></a></h4> <p>The <code>today</code> function creates a new <code>Illuminate\Support\Carbon</code> instance for the current date:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$today</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">today</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="method-trait-uses-recursive" class="collection-method"><a href="#method-trait-uses-recursive"><code>trait_uses_recursive()</code></a></h4> <p>The <code>trait_uses_recursive</code> function returns all traits used by a trait:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$traits</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">trait_uses_recursive</span><span style="color: #BFC7D5;">(\</span><span style="color: #82AAFF;">Illuminate</span><span style="color: #BFC7D5;">\</span><span style="color: #82AAFF;">Notifications</span><span style="color: #BFC7D5;">\</span><span style="color: #FFCB8B;">Notifiable</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-transform" class="collection-method"><a href="#method-transform"><code>transform()</code></a></h4> <p>The <code>transform</code> function executes a closure on a given value if the value is not <a href="#method-blank">blank</a> and then returns the return value of the closure:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$callback</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">};</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">transform</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">callback</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div></code></pre> <p>A default value or closure may be passed as the third argument to the function. This value will be returned if the given value is blank:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">transform</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">callback</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">The value is blank</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> The value is blank</span></div></code></pre> <h4 id="method-validator" class="collection-method"><a href="#method-validator"><code>validator()</code></a></h4> <p>The <code>validator</code> function creates a new <a href="/docs/11.x/validation">validator</a> instance with the given arguments. You may use it as an alternative to the <code>Validator</code> facade:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$validator</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">validator</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">data</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">rules</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">messages</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-value" class="collection-method"><a href="#method-value"><code>value()</code></a></h4> <p>The <code>value</code> function returns the value it is given. However, if you pass a closure to the function, the closure will be executed and its returned value will be returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">value</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> true</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">value</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> false</span></div></code></pre> <p>Additional arguments may be passed to the <code>value</code> function. If the first argument is a closure then the additional parameters will be passed to the closure as arguments, otherwise they will be ignored:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">value</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">name</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">return</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">name</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">},</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 'Taylor'</span></div></code></pre> <h4 id="method-view" class="collection-method"><a href="#method-view"><code>view()</code></a></h4> <p>The <code>view</code> function retrieves a <a href="/docs/11.x/views">view</a> instance:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">view</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">auth.login</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="method-with" class="collection-method"><a href="#method-with"><code>with()</code></a></h4> <p>The <code>with</code> function returns the value it is given. If a closure is passed as the second argument to the function, the closure will be executed and its returned value will be returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$callback</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">mixed</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">is_numeric</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">) </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">0</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">};</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">with</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">callback</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 10</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">with</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">callback</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 0</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">with</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> 5</span></div></code></pre> <h4 id="method-when" class="collection-method"><a href="#method-when"><code>when()</code></a></h4> <p>The <code>when</code> function returns the value it is given if a given condition evaluates to <code>true</code>. Otherwise, <code>null</code> is returned. If a closure is passed as the second argument to the function, the closure will be executed and its returned value will be returned:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Hello World</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">fn</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Hello World</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>The <code>when</code> function is primarily useful for conditionally rendering HTML attributes:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="blade" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> {!! </span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">condition</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">wire:poll="calculate"</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;"> !!}></span></div><div class='line'><span style="color: #BFC7D5;"> ...</span></div><div class='line'><span style="color: #89DDFF;"></</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div></code></pre> </div> <h2 id="other-utilities"><a href="#other-utilities">Other Utilities</a></h2> <h3 id="benchmarking"><a href="#benchmarking">Benchmarking</a></h3> <p>Sometimes you may wish to quickly test the performance of certain parts of your application. On those occasions, you may utilize the <code>Benchmark</code> support class to measure the number of milliseconds it takes for the given callbacks to complete:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #D3423E;"><?php</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Benchmark</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Benchmark</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">find</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 0.1 ms</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Benchmark</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Scenario 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">(), </span><span style="color: #697098;">//</span><span style="color: #697098;"> 0.5 ms</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Scenario 2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">(), </span><span style="color: #697098;">//</span><span style="color: #697098;"> 20.0 ms</span></div><div class='line'><span style="color: #BFC7D5;">]);</span></div></code></pre> <p>By default, the given callbacks will be executed once (one iteration), and their duration will be displayed in the browser / console.</p> <p>To invoke a callback more than once, you may specify the number of iterations that the callback should be invoked as the second argument to the method. When executing a callback more than once, the <code>Benchmark</code> class will return the average amount of milliseconds it took to execute the callback across all iterations:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #FFCB8B;">Benchmark</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dd</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">(), iterations: </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 0.5 ms</span></div></code></pre> <p>Sometimes, you may want to benchmark the execution of a callback while still obtaining the value returned by the callback. The <code>value</code> method will return a tuple containing the value returned by the callback and the amount of milliseconds it took to execute the callback:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$count</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$duration</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Benchmark</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">value</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">());</span></div></code></pre> <h3 id="dates"><a href="#dates">Dates</a></h3> <p>Laravel includes <a href="https://carbon.nesbot.com/docs/">Carbon</a>, a powerful date and time manipulation library. To create a new <code>Carbon</code> instance, you may invoke the <code>now</code> function. This function is globally available within your Laravel application:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$now</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">();</span></div></code></pre> </div> <p>Or, you may create a new <code>Carbon</code> instance using the <code>Illuminate\Support\Carbon</code> class:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Carbon</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$now</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Carbon</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">();</span></div></code></pre> </div> <p>For a thorough discussion of Carbon and its features, please consult the <a href="https://carbon.nesbot.com/docs/">official Carbon documentation</a>.</p> <h3 id="deferred-functions"><a href="#deferred-functions">Deferred Functions</a></h3> <div class="mb-10 max-w-2xl mx-auto px-4 py-8 shadow-lg lg:flex lg:items-center callout"> <div class="w-20 h-20 mb-6 flex items-center justify-center shrink-0 lg:mb-0 bg-red-600"> <div class="opacity-75"><svg width="6" height="35" viewBox="0 0 6 35" xmlns="http://www.w3.org/2000/svg"><title>exclamation</title><path d="M0 29h6v6H0v-6zM0 0h6v24H0V0z" fill="#FFF" fill-rule="nonzero"/></svg> </div> </div> <p class="mb-0 lg:ml-6 callout"> Deferred functions are currently in beta while we gather community feedback.</p> </div> <p>While Laravel's <a href="/docs/11.x/queues">queued jobs</a> allow you to queue tasks for background processing, sometimes you may have simple tasks you would like to defer without configuring or maintaining a long-running queue worker.</p> <p>Deferred functions allow you to defer the execution of a closure until after the HTTP response has been sent to the user, keeping your application feeling fast and responsive. To defer the execution of a closure, simply pass the closure to the <code>Illuminate\Support\defer</code> function:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Services\</span><span style="color: #FFCB8B;">Metrics</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Http\</span><span style="color: #FFCB8B;">Request</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Route</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">defer</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Route</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">post</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/orders</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Request</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Create order...</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">defer</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Metrics</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">reportOrder</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">order</span><span style="color: #BFC7D5;">));</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> </div> <p>By default, deferred functions will only be executed if the HTTP response, Artisan command, or queued job from which <code>Illuminate\Support\defer</code> is invoked completes successfully. This means that deferred functions will not be executed if a request results in a <code>4xx</code> or <code>5xx</code> HTTP response. If you would like a deferred function to always execute, you may chain the <code>always</code> method onto your deferred function:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">defer</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Metrics</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">reportOrder</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">order</span><span style="color: #BFC7D5;">))</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">always</span><span style="color: #BFC7D5;">();</span></div></code></pre> </div> <h4 id="cancelling-deferred-functions"><a href="#cancelling-deferred-functions">Cancelling Deferred Functions</a></h4> <p>If you need to cancel a deferred function before it is executed, you can use the <code>forget</code> method to cancel the function by its name. To name a deferred function, provide a second argument to the <code>Illuminate\Support\defer</code> function:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">defer</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Metrics</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">report</span><span style="color: #BFC7D5;">(),</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">reportMetrics</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;">defer</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">forget</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">reportMetrics</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div></code></pre> </div> <h4 id="deferred-function-compatibility"><a href="#deferred-function-compatibility">Deferred Function Compatibility</a></h4> <p>If you upgraded to Laravel 11.x from a Laravel 10.x application and your application's skeleton still contains an <code>app/Http/Kernel.php</code> file, you should add the <code>InvokeDeferredCallbacks</code> middleware to the beginning of the kernel's <code>$middleware</code> property:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight has-add-lines has-diff-lines' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$middleware</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> \Illuminate\Foundation\Http\Middleware\</span><span style="color: #C3E88D;">InvokeDeferredCallbacks</span><span style="color: #C3E88D;">::</span><span style="color: #C3E88D;">class</span><span style="color: #C3E88D;">, </span></div><div class='line'><span style="color: #BFC7D5;"> \App\Http\Middleware\</span><span style="color: #FFCB8B;">TrustProxies</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">];</span></div></code></pre> </div> <h4 id="disabling-deferred-functions-in-tests"><a href="#disabling-deferred-functions-in-tests">Disabling Deferred Functions in Tests</a></h4> <p>When writing tests, it may be useful to disable deferred functions. You may call <code>withoutDefer</code> in your test to instruct Laravel to invoke all deferred functions immediately:</p> <div class="tabbed-code" data-group="1df3cf502597c6d6b58a6e0bfc806498"> <div class="tabbed-code-nav"> <button class="tabbed-code-nav-button active" data-tab="Pest" onClick="setTab('Pest', '1df3cf502597c6d6b58a6e0bfc806498')">Pest</button> <button class="tabbed-code-nav-button" data-tab="PHPUnit" onClick="setTab('PHPUnit', '1df3cf502597c6d6b58a6e0bfc806498')">PHPUnit</button> </div> <div class="tabbed-code-body"> <div class="code-container active" data-tab="Pest"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">test</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">without defer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #FF5572;">this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withoutDefer</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> </div> <div class="code-container" data-tab="PHPUnit"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Tests\</span><span style="color: #FFCB8B;">TestCase</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">ExampleTest</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">TestCase</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">test_without_defer</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">void</span></div><div class='line'><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withoutDefer</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;"> }</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> </div> </div> </div> <p>If you would like to disable deferred functions for all tests within a test case, you may call the <code>withoutDefer</code> method from the <code>setUp</code> method on your base <code>TestCase</code> class:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight has-add-lines has-diff-lines' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #D3423E;"><?php</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">namespace</span><span style="color: #BFC7D5;"> Tests;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Foundation\Testing\</span><span style="color: #FFCB8B;">TestCase</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> BaseTestCase;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">abstract</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">TestCase</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">BaseTestCase</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">protected</span><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">function</span><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">setUp</span><span style="color: #C3E88D;">()</span><span style="color: #C3E88D;">:</span><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">void</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> {</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">parent</span><span style="color: #C3E88D;">::</span><span style="color: #C3E88D;">setUp</span><span style="color: #C3E88D;">();</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'> </div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> </span><span style="color: #C3E88D;">$this</span><span style="color: #C3E88D;">-></span><span style="color: #C3E88D;">withoutDefer</span><span style="color: #C3E88D;">();</span></div><div class='line line-add line-has-background' style='background-color: #99b76d23'><span style="color: #C3E88D;"> }</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> </div> <h3 id="lottery"><a href="#lottery">Lottery</a></h3> <p>Laravel's lottery class may be used to execute callbacks based on a set of given odds. This can be particularly useful when you only want to execute code for a percentage of your incoming requests:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Lottery</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">odds</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">)</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">winner</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">won</span><span style="color: #BFC7D5;">())</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">loser</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lost</span><span style="color: #BFC7D5;">())</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">choose</span><span style="color: #BFC7D5;">();</span></div></code></pre> <p>You may combine Laravel's lottery class with other Laravel features. For example, you may wish to only report a small percentage of slow queries to your exception handler. And, since the lottery class is callable, we may pass an instance of the class into any method that accepts callables:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Carbon\</span><span style="color: #FFCB8B;">CarbonInterval</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">DB</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Lottery</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">whenQueryingForLongerThan</span><span style="color: #BFC7D5;">(</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">CarbonInterval</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">),</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">odds</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">winner</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #82AAFF;">report</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Querying > 2 seconds.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)),</span></div><div class='line'><span style="color: #BFC7D5;">);</span></div></code></pre> <h4 id="testing-lotteries"><a href="#testing-lotteries">Testing Lotteries</a></h4> <p>Laravel provides some simple methods to allow you to easily test your application's lottery invocations:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Lottery will always win...</span></div><div class='line'><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">alwaysWin</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Lottery will always lose...</span></div><div class='line'><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">alwaysLose</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Lottery will win then lose, and finally return to normal behavior...</span></div><div class='line'><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fix</span><span style="color: #BFC7D5;">([</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">false</span><span style="color: #BFC7D5;">]);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Lottery will return to normal behavior...</span></div><div class='line'><span style="color: #FFCB8B;">Lottery</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">determineResultsNormally</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h3 id="pipeline"><a href="#pipeline">Pipeline</a></h3> <p>Laravel's <code>Pipeline</code> facade provides a convenient way to "pipe" a given input through a series of invokable classes, closures, or callables, giving each class the opportunity to inspect or modify the input and invoke the next callable in the pipeline:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Closure</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Pipeline</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Pipeline</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">)</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">through</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Closure</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$next</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$next</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;"> },</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Closure</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$next</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$next</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div><div class='line'><span style="color: #BFC7D5;"> },</span></div><div class='line'><span style="color: #BFC7D5;"> ])</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">then</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div></code></pre> </div> <p>As you can see, each invokable class or closure in the pipeline is provided the input and a <code>$next</code> closure. Invoking the <code>$next</code> closure will invoke the next callable in the pipeline. As you may have noticed, this is very similar to <a href="/docs/11.x/middleware">middleware</a>.</p> <p>When the last callable in the pipeline invokes the <code>$next</code> closure, the callable provided to the <code>then</code> method will be invoked. Typically, this callable will simply return the given input.</p> <p>Of course, as discussed previously, you are not limited to providing closures to your pipeline. You may also provide invokable classes. If a class name is provided, the class will be instantiated via Laravel's <a href="/docs/11.x/container">service container</a>, allowing dependencies to be injected into the invokable class:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Pipeline</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">)</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">through</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">GenerateProfilePhoto</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">ActivateSubscription</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">SendWelcomeEmail</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">,</span></div><div class='line'><span style="color: #BFC7D5;"> ])</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">then</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div></code></pre> </div> <h3 id="sleep"><a href="#sleep">Sleep</a></h3> <p>Laravel's <code>Sleep</code> class is a light-weight wrapper around PHP's native <code>sleep</code> and <code>usleep</code> functions, offering greater testability while also exposing a developer friendly API for working with time:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Sleep</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;">) {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/*</span><span style="color: #697098;"> ... </span><span style="color: #697098;">*/</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> <p>The <code>Sleep</code> class offers a variety of methods that allow you to work with different units of time:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Return a value after sleeping...</span></div><div class='line'><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">then</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Sleep while a given value is true...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">while</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">fn</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #BFC7D5;"> => </span><span style="color: #82AAFF;">shouldKeepSleeping</span><span style="color: #BFC7D5;">());</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Pause execution for 90 seconds...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1.5</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">minutes</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Pause execution for 2 seconds...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Pause execution for 500 milliseconds...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">milliseconds</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Pause execution for 5,000 microseconds...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5000</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">microseconds</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Pause execution until a given time...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">until</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addMinute</span><span style="color: #BFC7D5;">());</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Alias of PHP's native "sleep" function...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">sleep</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Alias of PHP's native "usleep" function...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">usleep</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5000</span><span style="color: #BFC7D5;">);</span></div></code></pre> <p>To easily combine units of time, you may use the <code>and</code> method:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">and</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">milliseconds</span><span style="color: #BFC7D5;">();</span></div></code></pre> <h4 id="testing-sleep"><a href="#testing-sleep">Testing Sleep</a></h4> <p>When testing code that utilizes the <code>Sleep</code> class or PHP's native sleep functions, your test will pause execution. As you might expect, this makes your test suite significantly slower. For example, imagine you are testing the following code:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/*</span><span style="color: #697098;"> ... </span><span style="color: #697098;">*/</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$seconds</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;">) {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$seconds</span><span style="color: #89DDFF;">++</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$waiting</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/*</span><span style="color: #697098;"> ... </span><span style="color: #697098;">*/</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> <p>Typically, testing this code would take <em>at least</em> one second. Luckily, the <code>Sleep</code> class allows us to "fake" sleeping so that our test suite stays fast:</p> <div class="tabbed-code" data-group="1df3cf502597c6d6b58a6e0bfc806498"> <div class="tabbed-code-nav"> <button class="tabbed-code-nav-button active" data-tab="Pest" onClick="setTab('Pest', '1df3cf502597c6d6b58a6e0bfc806498')">Pest</button> <button class="tabbed-code-nav-button" data-tab="PHPUnit" onClick="setTab('PHPUnit', '1df3cf502597c6d6b58a6e0bfc806498')">PHPUnit</button> </div> <div class="tabbed-code-body"> <div class="code-container active" data-tab="Pest"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">it</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">waits until ready</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> </div> <div class="code-container" data-tab="PHPUnit"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">test_it_waits_until_ready</span><span style="color: #D9F5DD;">()</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> </div> </div> </div> <p>When faking the <code>Sleep</code> class, the actual execution pause is by-passed, leading to a substantially faster test.</p> <p>Once the <code>Sleep</code> class has been faked, it is possible to make assertions against the expected "sleeps" that should have occurred. To illustrate this, let's imagine we are testing code that pauses execution three times, with each pause increasing by a single second. Using the <code>assertSequence</code> method, we can assert that our code "slept" for the proper amount of time while keeping our test fast:</p> <div class="tabbed-code" data-group="1df3cf502597c6d6b58a6e0bfc806498"> <div class="tabbed-code-nav"> <button class="tabbed-code-nav-button active" data-tab="Pest" onClick="setTab('Pest', '1df3cf502597c6d6b58a6e0bfc806498')">Pest</button> <button class="tabbed-code-nav-button" data-tab="PHPUnit" onClick="setTab('PHPUnit', '1df3cf502597c6d6b58a6e0bfc806498')">PHPUnit</button> </div> <div class="tabbed-code-body"> <div class="code-container active" data-tab="Pest"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #82AAFF;">it</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">checks if ready three times</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">function</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">()</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> //</span><span style="color: #697098;"> ...</span></div><div class='line'> </div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertSequence</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">]);</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> </div> <div class="code-container" data-tab="PHPUnit"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">test_it_checks_if_ready_three_times</span><span style="color: #D9F5DD;">()</span></div><div class='line'><span style="color: #BFC7D5;">{</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class='line'> </div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertSequence</span><span style="color: #BFC7D5;">([</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">seconds</span><span style="color: #BFC7D5;">(),</span></div><div class='line'><span style="color: #BFC7D5;"> ]);</span></div><div class='line'><span style="color: #BFC7D5;">}</span></div></code></pre> </div> </div> </div> <p>Of course, the <code>Sleep</code> class offers a variety of other assertions you may use when testing:</p> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Carbon\</span><span style="color: #FFCB8B;">CarbonInterval</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> Duration;</span></div><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\</span><span style="color: #FFCB8B;">Sleep</span><span style="color: #BFC7D5;">;</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Assert that sleep was called 3 times...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertSleptTimes</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Assert against the duration of sleep...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertSlept</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Duration</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$duration</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #697098;">/*</span><span style="color: #697098;"> ... </span><span style="color: #697098;">*/</span><span style="color: #BFC7D5;">;</span></div><div class='line'><span style="color: #BFC7D5;">}, times: </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Assert that the Sleep class was never invoked...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertNeverSlept</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #697098;">//</span><span style="color: #697098;"> Assert that, even if Sleep was called, no execution paused occurred...</span></div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">assertInsomniac</span><span style="color: #BFC7D5;">();</span></div></code></pre> <p>Sometimes it may be useful to perform an action whenever a fake sleep occurs in your application code. To achieve this, you may provide a callback to the <code>whenFakingSleep</code> method. In the following example, we use Laravel's <a href="/docs/11.x/mocking#interacting-with-time">time manipulation helpers</a> to instantly progress time by the duration of each sleep:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Carbon\</span><span style="color: #FFCB8B;">CarbonInterval</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> Duration;</span></div><div class='line'> </div><div class='line'><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">freezeTime</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">whenFakingSleep</span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Duration</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$duration</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Progress time when faking sleep...</span></div><div class='line'><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">travel</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$duration</span><span style="color: #89DDFF;">->totalMilliseconds</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">milliseconds</span><span style="color: #BFC7D5;">();</span></div><div class='line'><span style="color: #BFC7D5;">});</span></div></code></pre> </div> <p>As progressing time is a common requirement, the <code>fake</code> method accepts a <code>syncWithCarbon</code> argument to keep Carbon in sync when sleeping within a test:</p> <div class="code-container"> <pre><code data-theme="olaolu-palenight" data-lang="php" class='torchlight' style='background-color: #292D3E; --theme-selection-background: #7580B850;'><!-- Syntax highlighted by torchlight.dev --><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fake</span><span style="color: #BFC7D5;">(syncWithCarbon: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$start</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">now</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #FFCB8B;">Sleep</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">for</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">second</span><span style="color: #BFC7D5;">();</span></div><div class='line'> </div><div class='line'><span style="color: #BEC5D4;">$start</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffForHumans</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> 1 second ago</span></div></code></pre> </div> <p>Laravel uses the <code>Sleep</code> class internally whenever it is pausing execution. For example, the <a href="#method-retry"><code>retry</code></a> helper uses the <code>Sleep</code> class when sleeping, allowing for improved testability when using that helper.</p><script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script> function setTab(tab, group, save) { save = typeof save === 'undefined' ? true : save document .querySelectorAll(`.tabbed-code[data-group="${group}"] .tabbed-code-nav-button`) .forEach(el => el.classList.remove('active')) document .querySelectorAll(`.tabbed-code[data-group="${group}"] .code-container`) .forEach(el => el.classList.remove('active')) document .querySelectorAll(`.tabbed-code[data-group="${group}"] [data-tab="${tab}"]`) .forEach(el => el.classList.add('active')) if (save) { saveTab(tab, group) } } function getTabs() { try { return JSON.parse(localStorage.tabs) } catch { return {} } } function saveTab(tab, group) { localStorage.tabs = JSON.stringify({ ...getTabs(), [group]: tab, }) } function restoreTabs() { Object.entries(getTabs()).forEach(([group, tab]) => setTab(tab, group, false)) } restoreTabs() </script> <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CKYILK3E&placement=laravelcom" id="_carbonads_js"></script> </div> </section> </section> </div> </section> </div> </div> <footer class="relative pt-12 dark:bg-dark-700"> <div class="max-w-screen-2xl mx-auto w-full px-8"> <div> <a href="/" class="inline-flex"> <img class="w-16 h-16" src="/img/logomark.min.svg" alt="Laravel" width="64" height="64" loading="lazy"> </a> </div> <div class="mt-6 grid grid-cols-12 md:gap-x-8 gap-y-12 sm:mt-12"> <div class="col-span-12 lg:col-span-4"> <p class="max-w-sm text-xs text-gray-700 sm:text-sm dark:text-gray-500"> Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in most web projects.</p> <ul class="mt-6 flex items-center space-x-3"> <li> <a href="https://x.com/laravelphp"> <img class="hidden dark:inline-block w-6 h-6" src="/img/social/x.dark.min.svg" alt="X" width="24" height="20" loading="lazy"> <img class="inline-block dark:hidden w-6 h-6" src="/img/social/x.min.svg" alt="X" width="24" height="20" loading="lazy"> </a> </li> <li> <a href="https://github.com/laravel"> <img class="hidden dark:inline-block w-6 h-6" src="/img/social/github.dark.min.svg" alt="GitHub" width="24" height="24" loading="lazy"> <img class="inline-block dark:hidden w-6 h-6" src="/img/social/github.min.svg" alt="GitHub" width="24" height="24" loading="lazy"> </a> </li> <li> <a href="https://discord.gg/laravel"> <img class="hidden dark:inline-block w-6 h-6" src="/img/social/discord.dark.min.svg" alt="Discord" width="21" height="24" loading="lazy"> <img class="inline-block dark:hidden w-6 h-6" src="/img/social/discord.min.svg" alt="Discord" width="21" height="24" loading="lazy"> </a> </li> <li> <a href="https://www.youtube.com/laravelphp"> <img class="hidden dark:inline-block w-6 h-6" src="/img/social/youtube.dark.min.svg" alt="YouTube" width="169" height="150" loading="lazy"> <img class="inline-block dark:hidden w-6 h-6" src="/img/social/youtube.min.svg" alt="YouTube" width="169" height="150" loading="lazy"> </a> </li> </ul> </div> <div class="text-xs col-span-6 md:col-span-3 lg:col-span-2"> <span class="uppercase dark:text-gray-200">Highlights</span> <div class="mt-5"> <ul class="space-y-3 text-gray-700 dark:text-gray-500"> <li> <a href="/docs/11.x/releases" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Release Notes</a> </li> <li> <a href="/docs/11.x/installation" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Getting Started</a> </li> <li> <a href="/docs/11.x/routing" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Routing</a> </li> <li> <a href="/docs/11.x/blade" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Blade Templates</a> </li> <li> <a href="/docs/11.x/authentication" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Authentication</a> </li> <li> <a href="/docs/11.x/authorization" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Authorization</a> </li> <li> <a href="/docs/11.x/artisan" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Artisan Console</a> </li> <li> <a href="/docs/11.x/database" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Database</a> </li> <li> <a href="/docs/11.x/eloquent" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Eloquent ORM</a> </li> <li> <a href="/docs/11.x/testing" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Testing</a> </li> </ul> </div> </div> <div class="text-xs col-span-6 md:col-span-3 lg:col-span-2"> <span class="uppercase dark:text-gray-200">Resources</span> <div class="mt-5"> <ul class="space-y-3 text-gray-700 dark:text-gray-500"> <li> <a href="https://bootcamp.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laravel Bootcamp</a> </li> <li> <a href="https://laracasts.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laracasts</a> </li> <li> <a href="https://laravel-news.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laravel News</a> </li> <li> <a href="https://laracon.us" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laracon</a> </li> <li> <a href="https://laracon.au" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laracon AU</a> </li> <li> <a href="https://laracon.eu/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laracon EU</a> </li> <li> <a href="https://laracon.in/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Laracon India</a> </li> <li> <a href="https://larabelles.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Larabelles</a> </li> <li> <a href="https://laravel.com/careers" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Careers</a> </li> <li> <a href="https://larajobs.com/?partner=5" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Jobs</a> </li> <li> <a href="https://laracasts.com/discuss" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Forums</a> </li> <li> <a href="/trademark" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Trademark</a> </li> </ul> </div> </div> <div class="text-xs col-span-6 md:col-span-3 lg:col-span-2"> <span class="uppercase dark:text-gray-200">Partners</span> <div class="mt-5"> <ul class="space-y-3 text-gray-700 dark:text-gray-500"> <li> <a href="https://vehikl.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Vehikl</a> </li> <li> <a href="https://webreinvent.com/?utm_source=laravel&utm_medium=laravel.com&utm_campaign=footer-link" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">WebReinvent</a> </li> <li> <a href="https://tighten.co" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Tighten</a> </li> <li> <a href="https://www.bacancytechnology.com/hire-laravel-developer?utm_source=laravel&utm_medium=partners.laravel&utm_campaign=sponsors" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Bacancy</a> </li> <li> <a href="https://64robots.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">64 Robots</a> </li> <li> <a href="https://activelogic.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Active Logic</a> </li> <li> <a href="https://www.blackairplane.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Black Airplane</a> </li> <li> <a href="https://www.byte5.net/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Byte 5</a> </li> <li> <a href="https://www.curotec.com/services/technologies/laravel/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Curotec</a> </li> <li> <a href="https://www.cyber-duck.co.uk/how-we-work/technology/laravel?utm_source=Laravel%20Partner&utm_medium=Sponsorship" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Cyber-Duck</a> </li> <li> <a href="https://devsquad.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">DevSquad</a> </li> <li> <a href="https://jump24.co.uk/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Jump24</a> </li> <li> <a href="https://kirschbaumdevelopment.com/" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Kirschbaum</a> </li> </ul> </div> </div> <div class="text-xs col-span-6 md:col-span-3 lg:col-span-2"> <span class="uppercase dark:text-gray-200">Ecosystem</span> <div class="mt-5"> <ul class="space-y-3 text-gray-700 dark:text-gray-500"> <li> <a href="/docs/11.x/starter-kits#laravel-breeze" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Breeze</a> </li> <li> <a href="/docs/11.x/billing" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Cashier</a> </li> <li> <a href="/docs/11.x/dusk" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Dusk</a> </li> <li> <a href="/docs/11.x/broadcasting" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Echo</a> </li> <li> <a href="https://envoyer.io" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Envoyer</a> </li> <li> <a href="https://forge.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Forge</a> </li> <li> <a href="https://herd.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Herd</a> </li> <li> <a href="/docs/11.x/horizon" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Horizon</a> </li> <li> <a href="https://inertiajs.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Inertia</a> </li> <li> <a href="https://jetstream.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Jetstream</a> </li> <li> <a href="https://livewire.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Livewire</a> </li> <li> <a href="https://nova.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Nova</a> </li> <li> <a href="/docs/11.x/octane" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Octane</a> </li> <li> <a href="/docs/11.x/pennant" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Pennant</a> </li> <li> <a href="/docs/11.x/pint" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Pint</a> </li> <li> <a href="/docs/11.x/prompts" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Prompts</a> </li> <li> <a href="https://pulse.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Pulse</a> </li> <li> <a href="https://reverb.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Reverb</a> </li> <li> <a href="/docs/11.x/sail" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Sail</a> </li> <li> <a href="/docs/11.x/sanctum" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Sanctum</a> </li> <li> <a href="/docs/11.x/scout" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Scout</a> </li> <li> <a href="/docs/11.x/socialite" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Socialite</a> </li> <li> <a href="https://spark.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Spark</a> </li> <li> <a href="/docs/11.x/telescope" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Telescope</a> </li> <li> <a href="https://vapor.laravel.com" class="transition-colors hover:text-gray-600 dark:hover:text-gray-400">Vapor</a> </li> </ul> </div> </div> </div> <div class="mt-10 border-t pt-6 pb-16 border-gray-200 dark:border-dark-500"> <p class="text-xs text-gray-700 dark:text-gray-400"> Laravel is a Trademark of Laravel Holdings Inc.<br /> Copyright © 2011-2024 Laravel Holdings Inc. </p> <p class="mt-6 text-xs text-gray-700 dark:text-gray-400"> Code highlighting provided by <a href="https://torchlight.dev">Torchlight</a> </p> </div> </div> </footer> <script> var algolia_app_id = 'E3MIRNPJH5'; var algolia_search_key = '1fa3a8fec06eb1858d6ca137211225c0'; var version = '11.x'; </script> <script> var _gaq=[['_setAccount','UA-23865777-1'],['_trackPageview']]; (function(d,t){ var g=d.createElement(t),s=d.getElementsByTagName(t)[0]; g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js'; s.parentNode.insertBefore(g,s) }(document,'script')); </script> <!-- HubSpot --> <script type="text/javascript" id="hs-script-loader" async defer src="//js-na1.hs-scripts.com/45240648.js"></script> <div class="fixed"> <input type="text"> </div> </body> </html>