Seleccionar página

Artisan

Artisan Console

Introduction

Artisan is the command line interface included with Laravel. Artisan exists at the root of your
application as the artisan script and provides a number of helpful commands that
can assist you while you build your application. To view a list of all available
Artisan commands, you may use the list command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan list

Every command also includes a «help» screen which displays and describes the command’s available
arguments and options. To view a help screen, precede the name of the command with
help:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan help migrate

Laravel Sail

If you are using Laravel Sail as your local development
environment, remember to use the sail command line to invoke Artisan
commands. Sail will execute your Artisan commands within your application’s Docker containers:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">./vendor/bin/sail </span><span style="color: #BFC7D5;"><code>artisan list

Tinker (REPL)

Laravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.

Installation

All Laravel applications include Tinker by default. However, you may install Tinker using
Composer if you have previously removed it from your application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require laravel/tinker

lightbulb

Looking for hot reloading, multiline code editing, and autocompletion when interacting with
your Laravel application? Check out Tinkerwell!

Usage

Tinker allows you to interact with your entire Laravel application on the command line, including
your Eloquent models, jobs, events, and more. To enter the Tinker
environment, run the tinker Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan tinker

You can publish Tinker’s configuration file using the vendor:publish
command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

exclamation

The dispatch helper function and dispatch method on the
Dispatchable class depends on garbage collection to place the job on the queue.
Therefore, when using tinker, you should use Bus::dispatch or
Queue::push to dispatch jobs.

Command Allow List

Tinker utilizes an «allow» list to determine which Artisan commands are allowed to be run within
its shell. By default, you may run the clear-compiled, down,
env, inspire, migrate, migrate:install,
up, and optimize commands. If you would like to allow more commands
you may add them to the commands array in your tinker.php
configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">commands</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: #697098;">//</span><span style="color: #697098;"> App\Console\Commands\ExampleCommand::class,</span></div><div class="line"><span style="color: #BFC7D5;">],</span></div>

Classes
That Should Not Be Aliased

Typically, Tinker automatically aliases classes as you interact with them in Tinker. However, you
may wish to never alias some classes. You may accomplish this by listing the classes in the
dont_alias array of your tinker.php configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">dont_alias</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;">    App\Models\</span><span style="color: #FFCB8B;">User</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>

Writing Commands

In addition to the commands provided with Artisan, you may build your own custom commands.
Commands are typically stored in the app/Console/Commands directory; however, you
are free to choose your own storage location as long as your commands can be loaded by Composer.

Generating Commands

To create a new command, you may use the make:command Artisan command. This command
will create a new command class in the app/Console/Commands directory. Don’t worry
if this directory does not exist in your application – it will be created the first time you run
the make:command Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:command SendEmails

Command Structure

After generating your command, you should define appropriate values for the
signature and description properties of the class. These properties
will be used when displaying your command on the list screen. The
signature property also allows you to define your command’s input expectations. The
handle method will be called when your command is executed. You may place your
command logic in this method.

Let’s take a look at an example command. Note that we are able to request any dependencies we
need via the command’s handle method. The Laravel service
container
will automatically inject all dependencies that are type-hinted in this
method’s signature:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Console\Commands;
 
use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;
 
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user}';
 
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a marketing email to a user';
 
/**
* Execute the console command.
*/
public function handle(DripEmailer $drip): void
{
$drip->send(User::find($this->argument('user')));
}
}
lightbulb

For greater code reuse, it is good practice to keep your console commands light and let them
defer to application services to accomplish their tasks. In the example above, note that we
inject a service class to do the «heavy lifting» of sending the e-mails.

Exit Codes

If nothing is returned from the handle method and the command executes successfully,
the command will exit with a 0 exit code, indicating success. However, the
handle method may optionally return an integer to manually specify command’s exit
code:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</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;">Something went wrong.</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: #F78C6C;">1</span><span style="color: #BFC7D5;">;</span></div>

If you would like to «fail» the command from any method within the command, you may utilize the
fail method. The fail method will immediately terminate execution of
the command and return an exit code of 1:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">fail</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>

Closure Commands

Closure based commands provide an alternative to defining console commands as classes. In the
same way that route closures are an alternative to controllers, think
of command closures as an alternative to command classes.

Even though the routes/console.php file does not define HTTP routes, it
defines console based entry points (routes) into your application. Within this
file, you may define all of your closure based console commands using the
Artisan::command method. The command method accepts two arguments: the
command signature and a closure which receives the
command’s arguments and options:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">command</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user}</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: #C792EA;">string</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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">info</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">Sending email to: {</span><span style="color: #BEC5D4;">$user</span><span style="color: #C3E88D;">}!</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

The closure is bound to the underlying command instance, so you have full access to all of the
helper methods you would typically be able to access on a full command class.

Type-Hinting
Dependencies

In addition to receiving your command’s arguments and options, command closures may also
type-hint additional dependencies that you would like resolved out of the service container:

<!-- Syntax highlighted by torchlight.dev --><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;"> App\Support\</span><span style="color: #FFCB8B;">DripEmailer</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">command</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user}</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;">DripEmailer</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$drip</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">string</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: #BEC5D4;">$drip</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</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;">$user</span><span style="color: #BFC7D5;">));</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Closure Command
Descriptions

When defining a closure based command, you may use the purpose method to add a
description to the command. This description will be displayed when you run the
php artisan list or php artisan help commands:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">command</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user}</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: #C792EA;">string</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">purpose</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Send a marketing email to a user</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Isolatable Commands

exclamation

To utilize this feature, your application must be using the memcached,
redis, dynamodb, database, file, or
array cache driver as your application’s default cache driver. In addition, all
servers must be communicating with the same central cache server.

Sometimes you may wish to ensure that only one instance of a command can run at a time. To
accomplish this, you may implement the Illuminate\Contracts\Console\Isolatable
interface on your command class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;
 
class SendEmails extends Command implements Isolatable
{
// ...
}

When a command is marked as Isolatable, Laravel will automatically add an
--isolated option to the command. When the command is invoked with that option,
Laravel will ensure that no other instances of that command are already running. Laravel
accomplishes this by attempting to acquire an atomic lock using your application’s default cache
driver. If other instances of the command are running, the command will not execute; however,
the command will still exit with a successful exit status code:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 --isolated

If you would like to specify the exit status code that the command should return if it is not
able to execute, you may provide the desired status code via the isolated option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 --isolated=12

Lock ID

By default, Laravel will use the command’s name to generate the string key that is used to
acquire the atomic lock in your application’s cache. However, you may customize this key by
defining an isolatableId method on your Artisan command class, allowing you to
integrate the command’s arguments or options into the key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the isolatable ID for the command.</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;">isolatableId</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">argument</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: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Lock Expiration Time

By default, isolation locks expire after the command is finished. Or, if the command is
interrupted and unable to finish, the lock will expire after one hour. However, you may adjust
the lock expiration time by defining a isolationLockExpiresAt method on your
command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">DateTimeInterface</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">DateInterval</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;"> * Determine when an isolation lock expires for the command.</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;">isolationLockExpiresAt</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">DateTimeInterface</span><span style="color: #BFC7D5;">|</span><span style="color: #FFCB6B;">DateInterval</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;">now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addMinutes</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></div>

Defining Input
Expectations

When writing console commands, it is common to gather input from the user through arguments or
options. Laravel makes it very convenient to define the input you expect from the user using the
signature property on your commands. The signature property allows you
to define the name, arguments, and options for the command in a single, expressive,
route-like syntax.

Arguments

All user supplied arguments and options are wrapped in curly braces. In the following example,
the command defines one required argument: user:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name and signature of the console command.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@var</span><span style="color: #697098;"> </span><span style="color: #C792EA;">string</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$signature</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;">mail:send {user}</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div>

You may also make arguments optional or define default values for arguments:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Optional argument...</span></div><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user?}</span><span style="color: #D9F5DD;">'</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Optional argument with default value...</span></div><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user=foo}</span><span style="color: #D9F5DD;">'</span></div>

Options

Options, like arguments, are another form of user input. Options are prefixed by two hyphens
(--) when they are provided via the command line. There are two types of options:
those that receive a value and those that don’t. Options that don’t receive a value serve as a
boolean «switch». Let’s take a look at an example of this type of option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name and signature of the console command.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@var</span><span style="color: #697098;"> </span><span style="color: #C792EA;">string</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$signature</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;">mail:send {user} {--queue}</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div>

In this example, the --queue switch may be specified when calling the Artisan
command. If the --queue switch is passed, the value of the option will be
true. Otherwise, the value will be false:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 --queue

Options With Values

Next, let’s take a look at an option that expects a value. If the user must specify a value for
an option, you should suffix the option name with a = sign:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name and signature of the console command.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@var</span><span style="color: #697098;"> </span><span style="color: #C792EA;">string</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$signature</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;">mail:send {user} {--queue=}</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div>

In this example, the user may pass a value for the option like so. If the option is not specified
when invoking the command, its value will be null:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 --queue=default

You may assign default values to options by specifying the default value after the option name.
If no option value is passed by the user, the default value will be used:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user} {--queue=default}</span><span style="color: #D9F5DD;">'</span></div>

Option Shortcuts

To assign a shortcut when defining an option, you may specify it before the option name and use
the | character as a delimiter to separate the shortcut from the full option name:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user} {--Q|queue}</span><span style="color: #D9F5DD;">'</span></div>

When invoking the command on your terminal, option shortcuts should be prefixed with a single
hyphen and no = character should be included when specifying a value for the
option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 -Qdefault

Input Arrays

If you would like to define arguments or options to expect multiple input values, you may use the
* character. First, let’s take a look at an example that specifies such an
argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user*}</span><span style="color: #D9F5DD;">'</span></div>

When calling this method, the user arguments may be passed in order to the command
line. For example, the following command will set the value of user to an array
with 1 and 2 as its values:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send 1 2

This * character can be combined with an optional argument definition to allow zero
or more instances of an argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {user?*}</span><span style="color: #D9F5DD;">'</span></div>

Option Arrays

When defining an option that expects multiple input values, each option value passed to the
command should be prefixed with the option name:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send {--id=*}</span><span style="color: #D9F5DD;">'</span></div>

Such a command may be invoked by passing multiple --id arguments:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan mail:send --id=1 --id=2

Input Descriptions

You may assign descriptions to input arguments and options by separating the argument name from
the description using a colon. If you need a little extra room to define your command, feel free
to spread the definition across multiple lines:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name and signature of the console command.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@var</span><span style="color: #697098;"> </span><span style="color: #C792EA;">string</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$signature</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;">mail:send</span></div><div class="line"><span style="color: #C3E88D;">                        {user : The ID of the user}</span></div><div class="line"><span style="color: #C3E88D;">                        {--queue : Whether the job should be queued}</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div>

Prompting for Missing
Input

If your command contains required arguments, the user will receive an error message when they are
not provided. Alternatively, you may configure your command to automatically prompt
the user when required arguments are missing by implementing the
PromptsForMissingInput interface:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
 
class SendEmails extends Command implements PromptsForMissingInput
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user}';
 
// ...
}

If Laravel needs to gather a required argument from the user, it will automatically ask the user
for the argument by intelligently phrasing the question using either the argument name or
description. If you wish to customize the question used to gather the required argument, you may
implement the promptForMissingArgumentsUsing method, returning an array of
questions keyed by the argument names:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Prompt for missing input arguments using the returned questions.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">string</span><span style="color: #697098;">, string></span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">promptForMissingArgumentsUsing</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user</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;">Which user ID should receive the mail?</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>

You may also provide placeholder text by using a tuple containing the question and placeholder:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</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</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;">Which user ID should receive the mail?</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">E.g. 123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">];</span></div>

If you would like complete control over the prompt, you may provide a closure that should prompt
the user and return their answer:

<!-- Syntax highlighted by torchlight.dev --><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;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> Laravel\Prompts\</span><span style="color: #FFCB8B;">search</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">return</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</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: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #82AAFF;">        label</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Search for a user:</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;">        placeholder</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">E.g. Taylor Otwell</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;">        options</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: #BFC7D5;">$</span><span style="color: #BEC5D4;">value</span><span style="color: #D9F5DD;">)</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">=></span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">strlen</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">)</span><span style="color: #82AAFF;"> </span><span style="color: #C792EA;">></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">0</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">?</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</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;">like</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;">%</span><span style="color: #BFC7D5;">{$</span><span style="color: #BEC5D4;">value</span><span style="color: #BFC7D5;">}</span><span style="color: #C3E88D;">%</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</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;">id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">:</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></div><div class="line"><span style="color: #BFC7D5;">];</span></div>
lightbulb

The comprehensive Laravel Prompts documentation includes
additional information on the available prompts and their usage.

If you wish to prompt the user to select or enter options, you may include
prompts in your command’s handle method. However, if you only wish to prompt the
user when they have also been automatically prompted for missing arguments, then you may
implement the afterPromptingForMissingArguments method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Symfony\Component\Console\Input\</span><span style="color: #FFCB8B;">InputInterface</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Symfony\Component\Console\Output\</span><span style="color: #FFCB8B;">OutputInterface</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;"> Laravel\Prompts\</span><span style="color: #FFCB8B;">confirm</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><div class="line"> </div><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Perform actions after the user was prompted for missing arguments.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #C792EA;">protected</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">afterPromptingForMissingArguments</span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">InputInterface</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$input</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">OutputInterface</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$output</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: #BEC5D4;">$input</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">setOption</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">queue</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">confirm</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #82AAFF;">        label</span><span style="color: #BFC7D5;">:</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Would you like to queue the mail?</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;">        default</span><span style="color: #BFC7D5;">:</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;">option</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">queue</span><span style="color: #D9F5DD;">'</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>

Command I/O

Retrieving Input

While your command is executing, you will likely need to access the values for the arguments and
options accepted by your command. To do so, you may use the argument and
option methods. If an argument or option does not exist, null will be
returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Execute the console command.</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;">handle</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: #BEC5D4;">$userId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">argument</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: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you need to retrieve all of the arguments as an array, call the
arguments method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$arguments</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">arguments</span><span style="color: #BFC7D5;">();</span></div>

Options may be retrieved just as easily as arguments using the option method. To
retrieve all of the options as an array, call the options method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve a specific option...</span></div><div class="line"><span style="color: #BEC5D4;">$queueName</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">option</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">queue</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;"> Retrieve all options as an array...</span></div><div class="line"><span style="color: #BEC5D4;">$options</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">options</span><span style="color: #BFC7D5;">();</span></div>

Prompting for Input

lightbulb

Laravel Prompts is a PHP package for adding beautiful and
user-friendly forms to your command-line applications, with browser-like features including
placeholder text and validation.

In addition to displaying output, you may also ask the user to provide input during the execution
of your command. The ask method will prompt the user with the given question,
accept their input, and then return the user’s input back to your command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Execute the console command.</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;">handle</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: #BEC5D4;">$name</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ask</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</span><span style="color: #D9F5DD;">'</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>

The ask method also accepts an optional second argument which specifies the default
value that should be returned if no user input is provided:

<!-- Syntax highlighted by torchlight.dev --><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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ask</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</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></div>

The secret method is similar to ask, but the user’s input will not be
visible to them as they type in the console. This method is useful when asking for sensitive
information such as passwords:

<!-- 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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">secret</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is the password?</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Asking for Confirmation

If you need to ask the user for a simple «yes or no» confirmation, you may use the
confirm method. By default, this method will return false. However, if
the user enters y or yes in response to the prompt, the method will
return true.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">confirm</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Do you wish to continue?</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>

If necessary, you may specify that the confirmation prompt should return true by
default by passing true as the second argument to the confirm method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">confirm</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Do you wish to continue?</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">true</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>

Auto-Completion

The anticipate method can be used to provide auto-completion for possible choices.
The user can still provide any answer, regardless of the auto-completion hints:

<!-- Syntax highlighted by torchlight.dev --><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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">anticipate</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</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;">Dayle</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div>

Alternatively, you may pass a closure as the second argument to the anticipate
method. The closure will be called each time the user types an input character. The closure
should accept a string parameter containing the user’s input so far, and return an array of
options for auto-completion:

<!-- Syntax highlighted by torchlight.dev --><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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">anticipate</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your address?</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: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$input</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;"> Return auto-completion options...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Multiple Choice
Questions

If you need to give the user a predefined set of choices when asking a question, you may use the
choice method. You may set the array index of the default value to be returned if
no option is chosen by passing the index as the third argument to the method:

<!-- Syntax highlighted by torchlight.dev --><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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">choice</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</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;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Dayle</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$defaultIndex</span></div><div class="line"><span style="color: #BFC7D5;">);</span></div>

In addition, the choice method accepts optional fourth and fifth arguments for
determining the maximum number of attempts to select a valid response and whether multiple
selections are permitted:

<!-- Syntax highlighted by torchlight.dev --><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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">choice</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</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;">Taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Dayle</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$defaultIndex</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$maxAttempts</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$allowMultipleSelections</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">false</span></div><div class="line"><span style="color: #BFC7D5;">);</span></div>

Writing Output

To send output to the console, you may use the line, info,
comment, question, warn, and error methods.
Each of these methods will use appropriate ANSI colors for their purpose. For example, let’s
display some general information to the user. Typically, the info method will
display in the console as green colored text:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Execute the console command.</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;">handle</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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">info</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">The command was successful!</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

To display an error message, use the error method. Error message text is typically
displayed in red:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</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;">Something went wrong!</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may use the line method to display plain, uncolored text:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">line</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Display this on the screen</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may use the newLine method to display a blank line:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Write a single blank line...</span></div><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newLine</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Write three blank lines...</span></div><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newLine</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">);</span></div>

Tables

The table method makes it easy to correctly format multiple rows / columns of data.
All you need to do is provide the column names and the data for the table and Laravel will
automatically calculate the appropriate width and height of the table for you:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">table</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><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: #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;">email</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;">);</span></div>

Progress Bars

For long running tasks, it can be helpful to show a progress bar that informs users how complete
the task is. Using the withProgressBar method, Laravel will display a progress bar
and advance its progress for each iteration over a given iterable value:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withProgressBar</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: #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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">performTask</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>

Sometimes, you may need more manual control over how a progress bar is advanced. First, define
the total number of steps the process will iterate through. Then, advance the progress bar after
processing each item:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$bar</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->output-></span><span style="color: #82AAFF;">createProgressBar</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">users</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$bar</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">start</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">performTask</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: #BFC7D5;"> </span><span style="color: #BEC5D4;">$bar</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">advance</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;">$bar</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">finish</span><span style="color: #BFC7D5;">();</span></div>
lightbulb

For more advanced options, check out the Symfony
Progress Bar component documentation
.

Registering Commands

By default, Laravel automatically registers all commands within the
app/Console/Commands directory. However, you can instruct Laravel to scan other
directories for Artisan commands using the withCommands method in your
application’s bootstrap/app.php file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withCommands</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">__DIR__</span><span style="color: #89DDFF;">.</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/../app/Domain/Orders/Commands</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">])</span></div>

If necessary, you may also manually register commands by providing the command’s class name to
the withCommands method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Domain\Orders\Commands\</span><span style="color: #FFCB8B;">SendEmails</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withCommands</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">SendEmails</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>

When Artisan boots, all the commands in your application will be resolved by the service container and registered with Artisan.

Programmatically Executing Commands

Sometimes you may wish to execute an Artisan command outside of the CLI. For example, you may
wish to execute an Artisan command from a route or controller. You may
use the call method on the Artisan facade to accomplish this. The
call method accepts either the command’s signature name or class name as its first
argument, and an array of command parameters as the second argument. The exit code will be
returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Artisan</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;">/user/{user}/mail</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: #C792EA;">string</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: #BEC5D4;">$exitCode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">call</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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</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;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">--queue</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;">default</span><span style="color: #D9F5DD;">'</span></div><div class="line"><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>

Alternatively, you may pass the entire Artisan command to the call method as a
string:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">call</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send 1 --queue=default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Passing Array Values

If your command defines an option that accepts an array, you may pass an array of values to that
option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Artisan</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;">/mail</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: #BEC5D4;">$exitCode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">call</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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;">--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;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">13</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>

Passing Boolean Values

If you need to specify the value of an option that does not accept string values, such as the
--force flag on the migrate:refresh command, you should pass
true or false as the value of the option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$exitCode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">call</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">migrate:refresh</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;">--force</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;">true</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Queueing Artisan
Commands

Using the queue method on the Artisan facade, you may even queue
Artisan commands so they are processed in the background by your queue
workers
. Before using this method, make sure you have configured your queue
and are running a queue listener:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Artisan</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;">/user/{user}/mail</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: #C792EA;">string</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: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">queue</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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</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;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">--queue</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;">default</span><span style="color: #D9F5DD;">'</span></div><div class="line"><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>

Using the onConnection and onQueue methods, you may specify the
connection or queue the Artisan command should be dispatched to:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">queue</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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</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;">--queue</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;">default</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onConnection</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>redis')->onQueue('commands');

Calling Commands From Other Commands

Sometimes you may wish to call other commands from an existing Artisan command. You may do so
using the call method. This call method accepts the command name and
an array of command arguments / options:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Execute the console command.</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;">handle</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;">call</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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</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;">--queue</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;">default</span><span style="color: #D9F5DD;">'</span></div><div class="line"><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>

If you would like to call another console command and suppress all of its output, you may use the
callSilently method. The callSilently method has the same signature as
the call method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">callSilently</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mail:send</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</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;">--queue</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;">default</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Signal Handling

As you may know, operating systems allow signals to be sent to running processes. For example,
the SIGTERM signal is how operating systems ask a program to terminate. If you wish
to listen for signals in your Artisan console commands and execute code when they occur, you may
use the trap method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Execute the console command.</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;">handle</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;">trap</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">SIGTERM</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: #FF5572;">$this</span><span style="color: #89DDFF;">->shouldKeepRunning</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"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> (</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->shouldKeepRunning</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><div class="line"><span style="color: #BFC7D5;">}</span></div>

To listen for multiple signals at once, you may provide an array of signals to the
trap method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trap</span><span style="color: #BFC7D5;">([</span><span style="color: #82AAFF;">SIGTERM</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">SIGQUIT</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;">$signal</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->shouldKeepRunning</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"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">signal</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> SIGTERM / SIGQUIT</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Stub Customization

The Artisan console’s make commands are used to create a variety of classes, such as
controllers, jobs, migrations, and tests. These classes are generated
using «stub» files that are populated with values based on your input. However, you may want to
make small changes to files generated by Artisan. To accomplish this, you may use the
stub:publish command to publish the most common stubs to your application so that
you can customize them:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan stub:publish

The published stubs will be located within a stubs directory in the root of your
application. Any changes you make to these stubs will be reflected when you generate their
corresponding classes using Artisan’s make commands.

Events

Artisan dispatches three events when running commands:
Illuminate\Console\Events\ArtisanStarting,
Illuminate\Console\Events\CommandStarting, and
Illuminate\Console\Events\CommandFinished. The ArtisanStarting event
is dispatched immediately when Artisan starts running. Next, the CommandStarting
event is dispatched immediately before a command runs. Finally, the CommandFinished
event is dispatched once a command finishes executing.


Authentication

Authentication

Introduction

Many web applications provide a way for their users to authenticate with the application and
«login». Implementing this feature in web applications can be a complex and potentially risky
endeavor. For this reason, Laravel strives to give you the tools you need to implement
authentication quickly, securely, and easily.

At its core, Laravel’s authentication facilities are made up of «guards» and «providers». Guards
define how users are authenticated for each request. For example, Laravel ships with a
session guard which maintains state using session storage and cookies.

Providers define how users are retrieved from your persistent storage. Laravel ships with support
for retrieving users using Eloquent and the database
query builder. However, you are free to define additional providers as needed for your
application.

Your application’s authentication configuration file is located at
config/auth.php. This file contains several well-documented options for tweaking
the behavior of Laravel’s authentication services.

lightbulb

Guards and providers should not be confused with «roles» and «permissions». To learn more
about authorizing user actions via permissions, please refer to the authorization documentation.

Starter Kits

Want to get started fast? Install a Laravel application starter
kit
in a fresh Laravel application. After migrating your database, navigate
your browser to /register or any other URL that is assigned to your application.
The starter kits will take care of scaffolding your entire authentication system!

Even if you choose not to use a starter kit in your final Laravel application, installing
the Laravel Breeze starter kit can be a
wonderful opportunity to learn how to implement all of Laravel’s authentication
functionality in an actual Laravel project.
Since Laravel Breeze creates
authentication controllers, routes, and views for you,
you can examine the code within these files to learn how Laravel’s authentication features may
be implemented.

Database Considerations

By default, Laravel includes an App\Models\User Eloquent
model
in your app/Models directory. This model
may be used with the default Eloquent authentication driver.

If your application is not using Eloquent, you may use the database authentication
provider which uses the Laravel query builder. If your application is using MongoDB, check out
MongoDB’s official Laravel
user authentication documentation
.

When building the database schema for the App\Models\User
model, make sure the password column is at least 60 characters in length. Of
course, the users table migration that is included in new Laravel
applications already creates a column that exceeds this length.

Also, you should verify that your users (or equivalent) table contains a nullable,
string remember_token column of 100 characters. This column will be used to store a
token for users that select the «remember me» option when logging into your application. Again,
the default users table migration that is included in new Laravel
applications already contains this column.

Ecosystem Overview

Laravel offers several packages related to authentication. Before continuing, we’ll
review the general authentication ecosystem in Laravel and discuss each package’s
intended purpose.

First, consider how authentication works. When using a web browser, a user will provide their
username and password via a login form. If these credentials are correct, the application will
store information about the authenticated user in the user’s session.
A cookie issued to the browser contains the session ID so that subsequent requests to the
application can associate the user with the correct session. After the session cookie is
received, the application will retrieve the session data based on the session ID, note that the
authentication information has been stored in the session, and will consider the user as
«authenticated».

When a remote service needs to authenticate to access an API, cookies are not typically used for
authentication because there is no web browser. Instead, the remote service sends an API token
to the API on each request. The application may validate the incoming token against a table of
valid API tokens and «authenticate» the request as being performed by the user associated with
that API token.

Laravel’s Built-in Browser
Authentication Services

Laravel includes built-in authentication and session services which are typically accessed via
the Auth and Session facades. These features provide cookie-based
authentication for requests that are initiated from web browsers. They provide methods that
allow you to verify a user’s credentials and authenticate the user. In addition, these services
will automatically store the proper authentication data in the user’s session and issue the
user’s session cookie. A discussion of how to use these services is contained within this
documentation.

Application Starter Kits

As discussed in this documentation, you can interact with these authentication services manually
to build your application’s own authentication layer. However, to help you get started more
quickly, we have released free packages that provide robust,
modern scaffolding of the entire authentication layer. These packages are Laravel Breeze, Laravel Jetstream, and Laravel Fortify.

Laravel Breeze is a simple, minimal implementation of all of Laravel’s authentication
features, including login, registration, password reset, email verification, and password
confirmation. Laravel Breeze’s view layer is comprised of simple Blade templates styled with Tailwind CSS. To get started, check out the
documentation on Laravel’s application starter kits.

Laravel Fortify is a headless authentication backend for Laravel that implements many of
the features found in this documentation, including cookie-based authentication as well as other
features such as two-factor authentication and email verification. Fortify provides the
authentication backend for Laravel Jetstream or may be used independently in combination with Laravel Sanctum to provide authentication for an SPA that needs to
authenticate with Laravel.

Laravel Jetstream is a robust application
starter kit that consumes and exposes Laravel Fortify’s authentication services with a
beautiful, modern UI powered by Tailwind CSS, Livewire, and / or Inertia. Laravel Jetstream includes optional support for
two-factor authentication, team support, browser session management, profile management, and
built-in integration with Laravel Sanctum to offer API token
authentication. Laravel’s API authentication offerings are discussed below.

Laravel’s API Authentication Services

Laravel provides two optional packages to assist you in managing API tokens and authenticating
requests made with API tokens: Passport and Sanctum. Please note that these libraries and Laravel’s built-in
cookie based authentication libraries are not mutually exclusive. These libraries primarily
focus on API token authentication while the built-in authentication services focus on cookie
based browser authentication. Many applications will use both Laravel’s built-in cookie based
authentication services and one of Laravel’s API authentication packages.

Passport

Passport is an OAuth2 authentication provider, offering a variety of OAuth2 «grant types» which
allow you to issue various types of tokens. In general, this is a robust and complex package for
API authentication. However, most applications do not require the complex features offered by
the OAuth2 spec, which can be confusing for both users and developers. In addition, developers
have been historically confused about how to authenticate SPA applications or mobile
applications using OAuth2 authentication providers like Passport.

Sanctum

In response to the complexity of OAuth2 and developer confusion, we set out to build a simpler,
more streamlined authentication package that could handle both first-party web requests from a
web browser and API requests via tokens. This goal was realized with the release of Laravel Sanctum, which should be considered the preferred and
recommended authentication package for applications that will be offering a first-party web UI
in addition to an API, or will be powered by a single-page application (SPA) that exists
separately from the backend Laravel application, or applications that offer a mobile client.

Laravel Sanctum is a hybrid web / API authentication package that can manage your application’s
entire authentication process. This is possible because when Sanctum based applications receive
a request, Sanctum will first determine if the request includes a session cookie that references
an authenticated session. Sanctum accomplishes this by calling Laravel’s built-in authentication
services which we discussed earlier. If the request is not being authenticated via a session
cookie, Sanctum will inspect the request for an API token. If an API token is present, Sanctum
will authenticate the request using that token. To learn more about this process, please consult
Sanctum’s «how it works» documentation.

Laravel Sanctum is the API package we have chosen to include with the Laravel Jetstream application starter kit because
we believe it is the best fit for the majority of web application’s authentication needs.

Summary and Choosing
Your Stack

In summary, if your application will be accessed using a browser and you are building a
monolithic Laravel application, your application will use Laravel’s built-in authentication
services.

Next, if your application offers an API that will be consumed by third parties, you will choose
between Passport or Sanctum to provide
API token authentication for your application. In general, Sanctum should be preferred when
possible since it is a simple, complete solution for API authentication, SPA authentication, and
mobile authentication, including support for «scopes» or «abilities».

If you are building a single-page application (SPA) that will be powered by a Laravel backend,
you should use Laravel Sanctum. When using Sanctum, you will either
need to manually implement your own backend authentication
routes
or utilize Laravel Fortify as a headless
authentication backend service that provides routes and controllers
for features such as registration, password reset, email verification, and more.

Passport may be chosen when your application absolutely needs all of the features provided by the
OAuth2 specification.

And, if you would like to get started quickly, we are pleased to recommend Laravel Breeze as a quick way to start a new
Laravel application that already uses our preferred authentication stack of Laravel’s built-in
authentication services and Laravel Sanctum.

Authentication
Quickstart

exclamation

This portion of the documentation discusses authenticating users via the Laravel application starter kits, which includes UI
scaffolding to help you get started quickly. If you would like to integrate with Laravel’s
authentication systems directly, check out the documentation on manually authenticating users.

Install a Starter Kit

First, you should install a Laravel application starter kit. Our
current starter kits, Laravel Breeze and Laravel Jetstream, offer beautifully designed starting
points for incorporating authentication into your fresh Laravel application.

Laravel Breeze is a minimal, simple implementation of all of Laravel’s authentication features,
including login, registration, password reset, email verification, and password confirmation.
Laravel Breeze’s view layer is made up of simple Blade
templates
styled with Tailwind CSS. Additionally,
Breeze provides scaffolding options based on Livewire or Inertia, with the choice of using Vue or React for the
Inertia-based scaffolding.

Laravel Jetstream is a more robust application
starter kit that includes support for scaffolding your application with Livewire or Inertia and Vue. In addition, Jetstream features optional
support for two-factor authentication, teams, profile management, browser session management,
API support via Laravel Sanctum, account deletion, and more.

Retrieving
the Authenticated User

After installing an authentication starter kit and allowing users to register and authenticate
with your application, you will often need to interact with the currently authenticated user.
While handling an incoming request, you may access the authenticated user via the
Auth facade’s user method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve the currently authenticated user...</span></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;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve the currently authenticated user's ID...</span></div><div class="line"><span style="color: #BEC5D4;">$id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">id</span><span style="color: #BFC7D5;">();</span></div>

Alternatively, once a user is authenticated, you may access the authenticated user via an
Illuminate\Http\Request instance. Remember, type-hinted classes will automatically
be injected into your controller methods. By type-hinting the
Illuminate\Http\Request object, you may gain convenient access to the authenticated
user from any controller method in your application via the request’s
user method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
 
class FlightController extends Controller
{
/**
* Update the flight information for an existing flight.
*/
public function update(Request $request): RedirectResponse
{
$user = $request->user();
 
// ...
 
return redirect('/flights');
}
}

Determining if the Current User is
Authenticated

To determine if the user making the incoming HTTP request is authenticated, you may use the
check method on the Auth facade. This method will return
true if the user is authenticated:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">check</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user is logged in...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
lightbulb

Even though it is possible to determine if a user is authenticated using the
check method, you will typically use a middleware to verify that
the user is authenticated before allowing the user access to certain routes /
controllers. To learn more about this, check out the documentation on protecting routes.

Protecting Routes

Route middleware can be used to only allow
authenticated users to access a given route. Laravel ships with an
auth middleware, which is a middleware alias for the
Illuminate\Auth\Middleware\Authenticate class. Since this middleware
is already aliased internally by Laravel, all you need to do is attach the
middleware to a route definition:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/flights</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;"> Only authenticated users may access this <code>route...
})->middleware('auth');

Redirecting
Unauthenticated Users

When the auth middleware detects an unauthenticated user, it will
redirect the user to the login named
route
. You may modify this behavior using the method
redirectGuestsTo of your application’s bootstrap/app.php file:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withMiddleware</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;">Middleware</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$<code>middleware) {
$middleware->redirectGuestsTo('/login');
 
// Using a closure...
$middleware->redirectGuestsTo(fn (Request $request) => route('login'));
})

Specifying a Guard

When attaching the auth middleware to a route, you may
also specify which «guard» should be used to authenticate the user. The guard specified should
correspond to one of the keys in the guards array of your auth.php
configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/flights</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;"> Only authenticated users may access this <code>route...
})->middleware('auth:admin');

Login Throttling

If you are using the Laravel Breeze or Laravel Jetstream starter
kits
, rate limiting will automatically be applied to login attempts. By default, the
user will not be able to login for one minute if they fail to provide the correct credentials
after several attempts. The throttling is unique to the user’s username / email address and
their IP address.

lightbulb

If you would like to rate limit other routes in your application, check out the
rate limiting documentation.

Manually Authenticating Users

You are not required to use the authentication scaffolding included with Laravel’s application starter kits. If you choose not to use this
scaffolding, you will need to manage user authentication using the Laravel authentication
classes directly. Don’t worry, it’s a cinch!

We will access Laravel’s authentication services via the Auth facade, so we’ll need to make sure to import the Auth
facade at the top of the class. Next, let’s check out the attempt method. The
attempt method is normally used to handle authentication attempts from your
application’s «login» form. If authentication is successful, you should regenerate the user’s session to prevent session fixation:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
 
class LoginController extends Controller
{
/**
* Handle an authentication attempt.
*/
public function authenticate(Request $request): RedirectResponse
{
$credentials = $request->validate([
'email' => ['required', 'email'],
'password' => ['required'],
]);
 
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
 
return redirect()->intended('dashboard');
}
 
return back()->withErrors([
'email' => 'The provided credentials do not match our records.',
])->onlyInput('email');
}
}

The attempt method accepts an array of key / value pairs as its first argument. The
values in the array will be used to find the user in your database table. So, in
the example above, the user will be retrieved by the value of the email column. If
the user is found, the hashed password stored in the database will be compared with
the password value passed to the method via the array. You should not hash the
incoming request’s password value, since the framework will automatically hash the
value before comparing it to the hashed password in the database. An authenticated
session will be started for the user if the two hashed passwords match.

Remember, Laravel’s authentication services will retrieve users from your database
based on your authentication guard’s «provider» configuration. In the default
config/auth.php configuration file, the Eloquent user provider is
specified and it is instructed to use the App\Models\User model when
retrieving users. You may change these values within your configuration file based
on the needs of your application.

The attempt method will return true if authentication was successful.
Otherwise, false will be returned.

The intended method provided by Laravel’s redirector will redirect the user to the
URL they were attempting to access before being intercepted by the authentication
middleware. A fallback URI may be given to this method in case the intended
destination is not available.

Specifying
Additional Conditions

If you wish, you may also add extra query conditions to the authentication query in addition to
the user’s email and password. To accomplish this, we may simply add the query conditions to the
array passed to the attempt method. For example, we may verify that the user is
marked as «active»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">attempt</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;">$email</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">password</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;">$password</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">active</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Authentication was successful...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

For complex query conditions, you may provide a closure in your array of credentials. This
closure will be invoked with the query instance, allowing you to customize the query based on
your application’s needs:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Database\Eloquent\</span><span style="color: #FFCB8B;">Builder</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">attempt</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">password</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;">$password</span><span style="color: #BFC7D5;">,</span></div><div class="line"><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;">Builder</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$query</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$query</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">activeSubscription</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><span style="color: #697098;">//</span><span style="color: #697098;"> Authentication was successful...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
exclamation

In these examples, email is not a required option, it is merely used as an
example. You should use whatever column name corresponds to a «username» in your
database table.

The attemptWhen method, which receives a closure as its second argument, may be used
to perform more extensive inspection of the potential user before actually authenticating the
user. The closure receives the potential user and should return true or
false to indicate if the user may be authenticated:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">attemptWhen</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">password</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;">$password</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: #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;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotBanned</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: #697098;">//</span><span style="color: #697098;"> Authentication was successful...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Accessing
Specific Guard Instances

Via the Auth facade’s guard method, you may specify which guard
instance you would like to utilize when authenticating the user. This allows you to manage
authentication for separate parts of your application using entirely separate authenticatable
models or user tables.

The guard name passed to the guard method should correspond to one of the guards
configured in your auth.php configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guard</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;">attempt</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$credentials</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>

Remembering Users

Many web applications provide a «remember me» checkbox on their login form. If you would like to
provide «remember me» functionality in your application, you may pass a boolean value as the
second argument to the attempt method.

When this value is true, Laravel will keep the user authenticated indefinitely or
until they manually logout. Your users table must include the string
remember_token column, which will be used to store the «remember me» token. The
users table migration included with new Laravel applications already
includes this column:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">attempt</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;">$email</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">password</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;">$password</span><span style="color: #BFC7D5;">], </span><span style="color: #BEC5D4;">$remember</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user is being remembered...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If your application offers «remember me» functionality, you may use the viaRemember
method to determine if the currently authenticated user was authenticated using the «remember
me» cookie:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">viaRemember</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>

Other Authentication
Methods

Authenticate a User
Instance

If you need to set an existing user instance as the currently authenticated user, you may pass
the user instance to the Auth facade’s login method. The given user
instance must be an implementation of the Illuminate\Contracts\Auth\Authenticatable
contract. The App\Models\User model
included with Laravel already implements this interface. This method of authentication is useful
when you already have a valid user instance, such as directly after a user registers with your
application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">login</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div>

You may pass a boolean value as the second argument to the login method. This value
indicates if «remember me» functionality is desired for the authenticated session. Remember,
this means that the session will be authenticated indefinitely or until the user manually logs
out of the application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">login</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$remember</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>

If needed, you may specify an authentication guard before calling the login method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guard</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;">login</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">);</span></div>

Authenticate a User by
ID

To authenticate a user using their database record’s primary key, you may use the
loginUsingId method. This method accepts the primary key of the user you wish to
authenticate:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">loginUsingId</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">);</span></div>

You may pass a boolean value to the remember argument of the
loginUsingId method. This value indicates if «remember me» functionality is desired
for the authenticated session. Remember, this means that the session will be authenticated
indefinitely or until the user manually logs out of the application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">loginUsingId</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, remember: </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">);</span></div>

Authenticate a User Once

You may use the once method to authenticate a user with the application for a single
request. No sessions or cookies will be utilized when calling this method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">once</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$credentials</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>

HTTP Basic
Authentication

HTTP Basic Authentication
provides a quick way to authenticate users of your application without setting up a dedicated
«login» page. To get started, attach the auth.basic middleware to a route. The
auth.basic middleware is included with the Laravel framework, so you
do not need to define it:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/profile</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;"> Only authenticated users may access this <code>route...
})->middleware('auth.basic');

Once the middleware has been attached to the route, you will
automatically be prompted for credentials when accessing the route in your browser.
By default, the auth.basic middleware will assume the
email column on your users database table is the user’s
«username».

A Note on FastCGI

If you are using PHP FastCGI and Apache to serve your Laravel application, HTTP Basic
authentication may not work correctly. To correct these problems, the following lines may be
added to your application’s .htaccess file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">RewriteCond</span><span style="color: #BFC7D5;"> </span><span style="color: #80CBC4;">%{HTTP:Authorization}</span><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">^(.+)$</span></div><div class="line"><span style="color: #C792EA;">RewriteRule</span><span style="color: #BFC7D5;"> </span><span style="color: #80CBC4;">.*</span><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">-</span><span style="color: #BFC7D5;"> [E=HTTP_AUTHORIZATION:%{</span><span style="color: #FFCB6B;">HTTP:Authorization</span><span style="color: #BFC7D5;">}]</span></div>

Stateless HTTP Basic Authentication

You may also use HTTP Basic Authentication without setting a user identifier cookie in the
session. This is primarily helpful if you choose to use HTTP Authentication to authenticate
requests to your application’s API. To accomplish this, define a
middleware
that calls the onceBasic method. If no response is
returned by the onceBasic method, the request may be passed further into the
application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
 
class AuthenticateOnceWithBasicAuth
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return Auth::onceBasic() ?: $next($request);
}
 
}

Next, attach the middleware to a route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/api/user</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;"> Only authenticated users may access this <code>route...
})->middleware(AuthenticateOnceWithBasicAuth::class);

Logging Out

To manually log users out of your application, you may use the logout method
provided by the Auth facade. This will remove the authentication information from
the user’s session so that subsequent requests are not authenticated.

In addition to calling the logout method, it is recommended that you invalidate the
user’s session and regenerate their CSRF token. After logging the user
out, you would typically redirect the user to the root of your application:

<!-- Syntax highlighted by torchlight.dev --><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\Http\</span><span style="color: #FFCB8B;">RedirectResponse</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;">Auth</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;"> * Log the user out of the application.</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;">logout</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: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">RedirectResponse</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">logout</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invalidate</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">regenerateToken</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: #82AAFF;">redirect</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: #BFC7D5;">}</span></div>

Invalidating Sessions on Other Devices

Laravel also provides a mechanism for invalidating and «logging out» a user’s sessions that are
active on other devices without invalidating the session on their current device. This feature
is typically utilized when a user is changing or updating their password and you would like to
invalidate sessions on other devices while keeping the current device authenticated.

Before getting started, you should make sure that the
Illuminate\Session\Middleware\AuthenticateSession middleware is
included on the routes that should receive session authentication. Typically, you
should place this middleware on a route group definition so that it
can be applied to the majority of your application’s routes. By default, the
AuthenticateSession middleware may be attached to a route
using the auth.session middleware alias:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;"><code>middleware(['auth', 'auth.session'])->group(function () {
Route::get('/', function () {
// ...
});
});

Then, you may use the logoutOtherDevices method provided by the Auth
facade. This method requires the user to confirm their current password, which your application
should accept through an input form:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Auth</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">logoutOtherDevices</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$currentPassword</span><span style="color: #BFC7D5;">);</span></div>

When the logoutOtherDevices method is invoked, the user’s other sessions will be
invalidated entirely, meaning they will be «logged out» of all guards they were previously
authenticated by.

Password Confirmation

While building your application, you may occasionally have actions that should require the user
to confirm their password before the action is performed or before the user is redirected to a
sensitive area of the application. Laravel includes built-in middleware to make
this process a breeze. Implementing this feature will require you to define two
routes: one route to display a view asking the user to
confirm their password and another route to confirm that the password is valid and
redirect the user to their intended destination.

lightbulb

The following documentation discusses how to integrate with Laravel’s password confirmation
features directly; however, if you would like to get started more quickly, the Laravel application starter kits include support for this
feature!

Configuration

After confirming their password, a user will not be asked to confirm their password again for
three hours. However, you may configure the length of time before the user is
re-prompted for their password by changing the value of the password_timeout
configuration value within your application’s config/auth.php
configuration file.

Routing

The Password
Confirmation Form

First, we will define a route to display a view that requests the user
to confirm their password:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/confirm-password</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: #82AAFF;"><code>view('auth.confirm-password');
})->middleware('auth')->name('password.confirm');

As you might expect, the view that is returned by this route should
have a form containing a password field. In addition, feel free to include text
within the view that explains that the user is entering a protected area of the
application and must confirm their password.

Confirming the Password

Next, we will define a route that will handle the form request from the «confirm
password» view. This route will be responsible for validating the
password and redirecting the user to their intended destination:

<!-- Syntax highlighted by torchlight.dev --><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;">Hash</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;">Redirect</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;">/confirm-password</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: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #C792EA;">!</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Hash</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">check</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->password</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">user</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">->password</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: #82AAFF;">back</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withErrors</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">password</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;">The provided password does not match our records.</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: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">session</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">passwordConfirmed</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intended</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware(['auth', 'throttle:6,1']);

Before moving on, let’s examine this route in more detail. First, the request’s
password field is determined to actually match the authenticated user’s password.
If the password is valid, we need to inform Laravel’s session that the user has confirmed their
password. The passwordConfirmed method will set a timestamp in the user’s session
that Laravel can use to determine when the user last confirmed their password. Finally, we can
redirect the user to their intended destination.

Protecting Routes

You should ensure that any route that performs an action which requires recent
password confirmation is assigned the password.confirm middleware.
This middleware is included with the default installation of Laravel and will
automatically store the user’s intended destination in the session so that the user may be
redirected to that location after confirming their password. After storing the user’s intended
destination in the session, the middleware will redirect the user to the
password.confirm named route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/settings</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware(['password.confirm']);
 
Route::post('/settings', function () {
// ...
})->middleware(['password.confirm']);

Adding Custom Guards

You may define your own authentication guards using the extend method on the
Auth facade. You should place your call to the extend method within a
service provider. Since Laravel already ships with an
AppServiceProvider, we can place the code in that provider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Providers;
 
use App\Services\Auth\JwtGuard;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
// ...
 
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Auth::extend('jwt', function (Application $app, string $name, array $config) {
// Return an instance of Illuminate\Contracts\Auth\Guard...
 
return new JwtGuard(Auth::createUserProvider($config['provider']));
});
}
}

As you can see in the example above, the callback passed to the extend method should
return an implementation of Illuminate\Contracts\Auth\Guard. This interface
contains a few methods you will need to implement to define a custom guard. Once your custom
guard has been defined, you may reference the guard in the guards
configuration of your auth.php configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">guards</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;">api</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;">driver</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;">jwt</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;">provider</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;">users</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>

Closure Request Guards

The simplest way to implement a custom, HTTP request based authentication system is by using the
Auth::viaRequest method. This method allows you to quickly define your
authentication process using a single closure.

To get started, call the Auth::viaRequest method within the boot method
of your application’s AppServiceProvider. The viaRequest method
accepts an authentication driver name as its first argument. This name can be any string that
describes your custom guard. The second argument passed to the method should be a closure that
receives the incoming HTTP request and returns a user instance or, if authentication fails,
null:

<!-- Syntax highlighted by torchlight.dev --><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\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;">Auth</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;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">viaRequest</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">custom-token</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">token</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, (</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;">) </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->token</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</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>

Once your custom authentication driver has been defined, you may configure it as a
driver within the guards configuration of your auth.php
configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">guards</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;">api</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;">driver</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;">custom-token</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>

Finally, you may reference the guard when assigning the authentication middleware to
a route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;"><code>middleware('auth:api')->group(function () {
// ...
});

Adding Custom User
Providers

If you are not using a traditional relational database to store your users, you will
need to extend Laravel with your own authentication user provider. We will use the
provider method on the Auth facade to define a custom user provider.
The user provider resolver should return an implementation of
Illuminate\Contracts\Auth\UserProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Providers;
 
use App\Extensions\MongoUserProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
// ...
 
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Auth::provider('mongo', function (Application $app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
 
return new MongoUserProvider($app->make('mongo.connection'));
});
}
}

After you have registered the provider using the provider method, you may switch to
the new user provider in your auth.php configuration file. First,
define a provider that uses your new driver:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">providers</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;">users</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;">driver</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;">mongo</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>

Finally, you may reference this provider in your guards configuration:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">guards</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;">web</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;">driver</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;">session</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;">provider</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;">users</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>

The User Provider
Contract

Illuminate\Contracts\Auth\UserProvider implementations are responsible for fetching
an Illuminate\Contracts\Auth\Authenticatable implementation out of a persistent
storage system, such as MySQL, MongoDB, etc. These two interfaces allow the Laravel
authentication mechanisms to continue functioning regardless of how the user data is stored or
what type of class is used to represent the authenticated user:

Let’s take a look at the Illuminate\Contracts\Auth\UserProvider contract:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace Illuminate\Contracts\Auth;
 
interface UserProvider
{
public function retrieveById($identifier);
public function retrieveByToken($identifier, $token);
public function updateRememberToken(Authenticatable $user, $token);
public function retrieveByCredentials(array $credentials);
public function validateCredentials(Authenticatable $user, array $credentials);
public function rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false);
}

The retrieveById function typically receives a key representing the user, such as an
auto-incrementing ID from a MySQL database. The Authenticatable
implementation matching the ID should be retrieved and returned by the method.

The retrieveByToken function retrieves a user by their unique
$identifier and «remember me» $token, typically stored in a
database column like remember_token. As with the previous method, the
Authenticatable implementation with a matching token value should be returned by
this method.

The updateRememberToken method updates the $user instance’s
remember_token with the new $token. A fresh token is assigned to users
on a successful «remember me» authentication attempt or when the user is logging out.

The retrieveByCredentials method receives the array of credentials passed to the
Auth::attempt method when attempting to authenticate with an application. The
method should then «query» the underlying persistent storage for the user matching those
credentials. Typically, this method will run a query with a «where» condition that searches for
a user record with a «username» matching the value of $credentials['username']. The
method should return an implementation of Authenticatable. This method
should not attempt to do any password validation or authentication.

The validateCredentials method should compare the given $user with the
$credentials to authenticate the user. For example, this method will typically use
the Hash::check method to compare the value of
$user->getAuthPassword() to the value of $credentials['password'].
This method should return true or false indicating whether the
password is valid.

The rehashPasswordIfRequired method should rehash the given $user‘s
password if required and supported. For example, this method will typically use the
Hash::needsRehash method to determine if the $credentials['password']
value needs to be rehashed. If the password needs to be rehashed, the method should use the
Hash::make method to rehash the password and update the user’s record in the
underlying persistent storage.

The Authenticatable
Contract

Now that we have explored each of the methods on the UserProvider, let’s take a look
at the Authenticatable contract. Remember, user providers should return
implementations of this interface from the retrieveById,
retrieveByToken, and retrieveByCredentials methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace Illuminate\Contracts\Auth;
 
interface Authenticatable
{
public function getAuthIdentifierName();
public function getAuthIdentifier();
public function getAuthPasswordName();
public function getAuthPassword();
public function getRememberToken();
public function setRememberToken($value);
public function getRememberTokenName();
}

This interface is simple. The getAuthIdentifierName method should return the name of
the «primary key» column for the user and the getAuthIdentifier method should
return the «primary key» of the user. When using a MySQL back-end, this would likely be the
auto-incrementing primary key assigned to the user record. The getAuthPasswordName
method should return the name of the user’s password column. The getAuthPassword
method should return the user’s hashed password.

This interface allows the authentication system to work with any «user» class, regardless of what
ORM or storage abstraction layer you are using. By default, Laravel includes an
App\Models\User class in the app/Models directory which implements
this interface.

Automatic Password
Rehashing

Laravel’s default password hashing algorithm is bcrypt. The «work factor» for bcrypt hashes can
be adjusted via your application’s config/hashing.php configuration
file or the BCRYPT_ROUNDS environment variable.

Typically, the bcrypt work factor should be increased over time as CPU / GPU processing power
increases. If you increase the bcrypt work factor for your application, Laravel will gracefully
and automatically rehash user passwords as users authenticate with your application via
Laravel’s starter kits or when you manually authenticate
users
via the attempt method.

Typically, automatic password rehashing should not disrupt your application; however, you may
disable this behavior by publishing the hashing configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan config:publish hashing

Once the configuration file has been published, you may set the
rehash_on_login configuration value to false:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">rehash_on_login</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;">false</span><span style="color: #BFC7D5;">,</span></div>

Events

Laravel dispatches a variety of events during the authentication
process. You may define listeners for any of the following events:

Event Name
Illuminate\Auth\Events\Registered
Illuminate\Auth\Events\Attempting
Illuminate\Auth\Events\Authenticated
Illuminate\Auth\Events\Login
Illuminate\Auth\Events\Failed
Illuminate\Auth\Events\Validated
Illuminate\Auth\Events\Verified
Illuminate\Auth\Events\Logout
Illuminate\Auth\Events\CurrentDeviceLogout
Illuminate\Auth\Events\OtherDeviceLogout
Illuminate\Auth\Events\Lockout
Illuminate\Auth\Events\PasswordReset


Authorization

Authorization

Introduction

In addition to providing built-in authentication services,
Laravel also provides a simple way to authorize user actions against a given resource. For
example, even though a user is authenticated, they may not be authorized to update or delete
certain Eloquent models or database records managed by your
application. Laravel’s authorization features provide an easy, organized way of managing these
types of authorization checks.

Laravel provides two primary ways of authorizing actions: gates and policies. Think of gates and policies like routes
and controllers. Gates provide a simple, closure-based approach to authorization
while policies, like controllers, group logic around a particular
model or resource. In this documentation, we’ll explore gates first and then
examine policies.

You do not need to choose between exclusively using gates or exclusively using policies when
building an application. Most applications will most likely contain some mixture of gates and
policies, and that is perfectly fine! Gates are most applicable to actions that are not related
to any model or resource, such as viewing an administrator dashboard.
In contrast, policies should be used when you wish to authorize an action for a particular
model or resource.

Gates

Writing Gates

exclamation

Gates are a great way to learn the basics of Laravel’s authorization features; however, when
building robust Laravel applications you should consider using policies to organize your authorization rules.

Gates are simply closures that determine if a user is authorized to perform a given action.
Typically, gates are defined within the boot method of the
App\Providers\AppServiceProvider class using the Gate facade. Gates
always receive a user instance as their first argument and may optionally receive additional
arguments such as a relevant Eloquent model.

In this example, we’ll define a gate to determine if a user can update a given
App\Models\Post model. The gate will accomplish this by comparing the
user’s id against the user_id of the user that created the post:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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;">Gate</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;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</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;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #89DDFF;">->user_id</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>

Like controllers, gates may also be defined using a class callback array:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Policies\</span><span style="color: #FFCB8B;">PostPolicy</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;">Gate</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;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #FFCB8B;">PostPolicy</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Authorizing
Actions

To authorize an action using gates, you should use the allows or denies
methods provided by the Gate facade. Note that you are not required to pass the
currently authenticated user to these methods. Laravel will automatically take care of passing
the user into the gate closure. It is typical to call the gate authorization methods within your
application’s controllers before performing an action that requires authorization:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
 
class PostController extends Controller
{
/**
* Update the given post.
*/
public function update(Request $request, Post $post): RedirectResponse
{
if (! Gate::allows('update-post', $post)) {
abort(403);
}
 
// Update the post...
 
return redirect('/posts');
}
}

If you would like to determine if a user other than the currently authenticated user is
authorized to perform an action, you may use the forUser method on the
Gate facade:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forUser</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;">allows</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user can update the post...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forUser</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;">denies</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user can't update the post...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

You may authorize multiple actions at a time using the any or none
methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">any</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">delete-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user can update or delete the post...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">none</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">delete-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user can't update or delete the post...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Authorizing or Throwing Exceptions

If you would like to attempt to authorize an action and automatically throw an
Illuminate\Auth\Access\AuthorizationException if the user is not allowed to perform
the given action, you may use the Gate facade’s authorize method.
Instances of AuthorizationException are automatically converted to a 403 HTTP
response by Laravel:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">authorize</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The action is authorized...</span></div>

Supplying
Additional Context

The gate methods for authorizing abilities (allows, denies,
check, any, none, authorize,
can, cannot) and the authorization Blade directives (@can, @cannot,
@canany) can receive an array as their second argument. These array elements are
passed as parameters to the gate closure, and can be used for additional context when making
authorization decisions:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Category</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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create-post</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Category</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$category</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">bool</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$pinned</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #C792EA;">!</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canPublishToGroup</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$category</span><span style="color: #89DDFF;">->group</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: #82AAFF;">false</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">    } </span><span style="color: #C792EA;">elseif</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$pinned</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">&&</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">!</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canPinPosts</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: #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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">true</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: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">check</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create-post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #BEC5D4;">$category</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$pinned</span><span style="color: #BFC7D5;">])) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The user can create the post...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Gate Responses

So far, we have only examined gates that return simple boolean values. However, sometimes you may
wish to return a more detailed response, including an error message. To do so, you may return an
Illuminate\Auth\Access\Response from your gate:

<!-- Syntax highlighted by torchlight.dev --><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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">edit-settings</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;">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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->isAdmin</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">deny</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">You must be an administrator.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Even when you return an authorization response from your gate, the Gate::allows
method will still return a simple boolean value; however, you may use the
Gate::inspect method to get the full authorization response returned by the gate:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">inspect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">edit-settings</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$response</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">allowed</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The action is authorized...</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">else</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">echo</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$response</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">message</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

When using the Gate::authorize method, which throws an
AuthorizationException if the action is not authorized, the error message provided
by the authorization response will be propagated to the HTTP response:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">authorize</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">edit-settings</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 action is authorized...</span></div>

Customizing
The HTTP Response Status

When an action is denied via a Gate, a 403 HTTP response is returned; however, it
can sometimes be useful to return an alternative HTTP status code. You may customize the HTTP
status code returned for a failed authorization check using the denyWithStatus
static constructor on the Illuminate\Auth\Access\Response class:

<!-- Syntax highlighted by torchlight.dev --><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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">edit-settings</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;">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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->isAdmin</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">denyWithStatus</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">404</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Because hiding resources via a 404 response is such a common pattern for web
applications, the denyAsNotFound method is offered for convenience:

<!-- Syntax highlighted by torchlight.dev --><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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">define</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">edit-settings</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;">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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->isAdmin</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">denyAsNotFound</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Intercepting Gate Checks

Sometimes, you may wish to grant all abilities to a specific user. You may use the
before method to define a closure that is run before all other authorization
checks:

<!-- Syntax highlighted by torchlight.dev --><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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">before</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: #BFC7D5;">, </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$ability</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdministrator</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: #82AAFF;">true</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>

If the before closure returns a non-null result that result will be considered the
result of the authorization check.

You may use the after method to define a closure to be executed after all other
authorization checks:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">after</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: #BFC7D5;">, </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$ability</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">bool</span><span style="color: #BFC7D5;">|</span><span style="color: #C792EA;">null</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$result</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">mixed</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$arguments</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdministrator</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: #82AAFF;">true</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>

Values returned by after closures will not override the result of the authorization
check unless the gate or policy returned null.

Inline Authorization

Occasionally, you may wish to determine if the currently authenticated user is authorized to
perform a given action without writing a dedicated gate that corresponds to the action. Laravel
allows you to perform these types of «inline» authorization checks via the
Gate::allowIf and Gate::denyIf methods. Inline authorization does not
execute any defined «before» or «after» authorization
hooks
:

<!-- Syntax highlighted by torchlight.dev --><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;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allowIf</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: #89DDFF;">-></span><span style="color: #82AAFF;">isAdministrator</span><span style="color: #BFC7D5;">());</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">denyIf</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: #89DDFF;">-></span><span style="color: #82AAFF;">banned</span><span style="color: #BFC7D5;">());</span></div>

If the action is not authorized or if no user is currently authenticated, Laravel will
automatically throw an Illuminate\Auth\Access\AuthorizationException exception.
Instances of AuthorizationException are automatically converted to a 403 HTTP
response by Laravel’s exception handler.

Creating Policies

Generating Policies

Policies are classes that organize authorization logic around a particular model or
resource. For example, if your application is a blog, you may have an
App\Models\Post model and a corresponding
App\Policies\PostPolicy to authorize user actions such as creating or updating
posts.

You may generate a policy using the make:policy Artisan command. The generated
policy will be placed in the app/Policies directory. If this directory does not
exist in your application, Laravel will create it for you:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:policy PostPolicy

The make:policy command will generate an empty policy class. If you would like to
generate a class with example policy methods related to viewing, creating,
updating, and deleting the resource, you may provide a --model option when
executing the command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:policy PostPolicy --model=Post

Registering Policies

Policy Discovery

By default, Laravel automatically discover policies as long as the model and policy
follow standard Laravel naming conventions. Specifically, the policies must be in a
Policies directory at or above the directory that contains your
models. So, for example, the models may be placed in the
app/Models directory while the policies may be placed in the
app/Policies directory. In this situation, Laravel will check for policies in
app/Models/Policies then app/Policies. In addition, the policy name
must match the model name and have a Policy suffix. So, a
User model would correspond to a UserPolicy policy class.

If you would like to define your own policy discovery logic, you may register a custom policy
discovery callback using the Gate::guessPolicyNamesUsing method. Typically, this
method should be called from the boot method of your application’s
AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guessPolicyNamesUsing</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;">$<code>modelClass) {
// Return the name of the policy class for the given model...
});

Manually Registering
Policies

Using the Gate facade, you may manually register policies and their corresponding
models within the boot method of your application’s
AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Order</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Policies\</span><span style="color: #FFCB8B;">OrderPolicy</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;">Gate</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;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">policy</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">OrderPolicy</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>

Writing Policies

Policy Methods

Once the policy class has been registered, you may add methods for each action it authorizes. For
example, let’s define an update method on our PostPolicy which
determines if a given App\Models\User can update a given
App\Models\Post instance.

The update method will receive a User and a Post instance
as its arguments, and should return true or false indicating whether
the user is authorized to update the given Post. So, in this example, we will
verify that the user’s id matches the user_id on the post:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Policies;
 
use App\Models\Post;
use App\Models\User;
 
class PostPolicy
{
/**
* Determine if the given post can be updated by the user.
*/
public function update(User $user, Post $post): bool
{
return $user->id === $post->user_id;
}
}

You may continue to define additional methods on the policy as needed for the various actions it
authorizes. For example, you might define view or delete methods to
authorize various Post related actions, but remember you are free to give your
policy methods any name you like.

If you used the --model option when generating your policy via the Artisan console,
it will already contain methods for the viewAny, view,
create, update, delete, restore, and
forceDelete actions.

lightbulb

All policies are resolved via the Laravel service container,
allowing you to type-hint any needed dependencies in the policy’s constructor to have them
automatically injected.

Policy Responses

So far, we have only examined policy methods that return simple boolean values. However,
sometimes you may wish to return a more detailed response, including an error message. To do so,
you may return an Illuminate\Auth\Access\Response instance from your policy method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;"> * Determine if the given post can be updated by the user.</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;">update</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #89DDFF;">->user_id</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">deny</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">You do not own this post.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

When returning an authorization response from your policy, the Gate::allows method
will still return a simple boolean value; however, you may use the Gate::inspect
method to get the full authorization response returned by the gate:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Gate</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">inspect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$response</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">allowed</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The action is authorized...</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">else</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">echo</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$response</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">message</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

When using the Gate::authorize method, which throws an
AuthorizationException if the action is not authorized, the error message provided
by the authorization response will be propagated to the HTTP response:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">authorize</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The action is authorized...</span></div>

Customizing the HTTP Response Status

When an action is denied via a policy method, a 403 HTTP response is returned;
however, it can sometimes be useful to return an alternative HTTP status code. You may customize
the HTTP status code returned for a failed authorization check using the
denyWithStatus static constructor on the
Illuminate\Auth\Access\Response class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;"> * Determine if the given post can be updated by the user.</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;">update</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #89DDFF;">->user_id</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">denyWithStatus</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">404</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Because hiding resources via a 404 response is such a common pattern for web
applications, the denyAsNotFound method is offered for convenience:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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\Auth\Access\</span><span style="color: #FFCB8B;">Response</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;"> * Determine if the given post can be updated by the user.</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;">update</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #89DDFF;">->user_id</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">allow</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Response</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">denyAsNotFound</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Methods Without Models

Some policy methods only receive an instance of the currently authenticated user. This situation
is most common when authorizing create actions. For example, if you are creating a
blog, you may wish to determine if a user is authorized to create any posts at all. In these
situations, your policy method should only expect to receive a user instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Determine if the given user can create posts.</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;">create</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: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->role</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;">writer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Guest Users

By default, all gates and policies automatically return false if the incoming HTTP
request was not initiated by an authenticated user. However, you may allow these authorization
checks to pass through to your gates and policies by declaring an «optional» type-hint or
supplying a null default value for the user argument definition:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Policies;
 
use App\Models\Post;
use App\Models\User;
 
class PostPolicy
{
/**
* Determine if the given post can be updated by the user.
*/
public function update(?User $user, Post $post): bool
{
return $user?->id === $post->user_id;
}
}

Policy Filters

For certain users, you may wish to authorize all actions within a given policy. To accomplish
this, define a before method on the policy. The before method will be
executed before any other methods on the policy, giving you an opportunity to authorize the
action before the intended policy method is actually called. This feature is most commonly used
for authorizing application administrators to perform any action:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Perform pre-authorization checks.</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;">before</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: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$ability</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><span style="color: #C792EA;">null</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdministrator</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: #82AAFF;">true</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">null</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you would like to deny all authorization checks for a particular type of user then you may
return false from the before method. If null is returned,
the authorization check will fall through to the policy method.

exclamation

The before method of a policy class will not be called if the class doesn’t
contain a method with a name matching the name of the ability being checked.

Authorizing Actions Using Policies

Via the User Model

The App\Models\User model that is included with your Laravel
application includes two helpful methods for authorizing actions: can and
cannot. The can and cannot methods receive the name of
the action you wish to authorize and the relevant model. For example, let’s
determine if a user is authorized to update a given App\Models\Post
model. Typically, this will be done within a controller method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
 
class PostController extends Controller
{
/**
* Update the given post.
*/
public function update(Request $request, Post $post): RedirectResponse
{
if ($request->user()->cannot('update', $post)) {
abort(403);
}
 
// Update the post...
 
return redirect('/posts');
}
}

If a policy is registered for the given model,
the can method will automatically call the appropriate policy and return the
boolean result. If no policy is registered for the model, the can
method will attempt to call the closure-based Gate matching the given action name.

Actions That Don’t Require Models

Remember, some actions may correspond to policy methods like create that do not
require a model instance. In these situations, you may pass a class name to the
can method. The class name will be used to determine which policy to use when
authorizing the action:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
 
class PostController extends Controller
{
/**
* Create a post.
*/
public function store(Request $request): RedirectResponse
{
if ($request->user()->cannot('create', Post::class)) {
abort(403);
}
 
// Create the post...
 
return redirect('/posts');
}
}

Via the Gate Facade

In addition to helpful methods provided to the App\Models\User model,
you can always authorize actions via the Gate facade’s authorize
method.

Like the can method, this method accepts the name of the action you wish to
authorize and the relevant model. If the action is not authorized, the
authorize method will throw an
Illuminate\Auth\Access\AuthorizationException exception which the Laravel exception
handler will automatically convert to an HTTP response with a 403 status code:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
 
class PostController extends Controller
{
/**
* Update the given blog post.
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(Request $request, Post $post): RedirectResponse
{
Gate::authorize('update', $post);
 
// The current user can update the blog post...
 
return redirect('/posts');
}
}

Actions That Don’t Require Models

As previously discussed, some policy methods like create do not require a
model instance. In these situations, you should pass a class name to the
authorize method. The class name will be used to determine which policy to use when
authorizing the action:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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;">RedirectResponse</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;">Gate</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;"> * Create a new blog post.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@throws</span><span style="color: #697098;"> </span><span style="color: #697098;">\</span><span style="color: #697098;">Illuminate</span><span style="color: #697098;">\</span><span style="color: #697098;">Auth</span><span style="color: #697098;">\</span><span style="color: #697098;">Access</span><span style="color: #697098;">\</span><span style="color: #FFCB8B;">AuthorizationException</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;">create</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: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">RedirectResponse</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">authorize</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Post</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: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The current user can create blog posts...</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/posts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Via Middleware

Laravel includes a middleware that can authorize actions before the incoming request
even reaches your routes or controllers. By default, the
Illuminate\Auth\Middleware\Authorize middleware may be attached to a
route using the can middleware alias, which is
automatically registered by Laravel. Let’s explore an example of using the can
middleware to authorize that a user can update a post:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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;">put</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/post/{post}</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</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;"> The current user may update the post...</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware('can:update,post');

In this example, we’re passing the can middleware two arguments. The
first is the name of the action we wish to authorize and the second is the route
parameter we wish to pass to the policy method. In this case, since we are using implicit model binding, an
App\Models\Post model will be passed to the policy method. If the user
is not authorized to perform the given action, an HTTP response with a 403 status code will be
returned by the middleware.

For convenience, you may also attach the can middleware to your
route using the can method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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;">put</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/post/{post}</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</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;"> The current user may update the post...</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">post</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Actions That Don’t Require Models

Again, some policy methods like create do not require a model instance.
In these situations, you may pass a class name to the middleware. The class name
will be used to determine which policy to use when authorizing the action:

<!-- Syntax highlighted by torchlight.dev --><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;">/post</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;"> The current user may create posts...</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware('can:create,App\Models\Post');

Specifying the entire class name within a string middleware definition can become
cumbersome. For that reason, you may choose to attach the can
middleware to your route using the can method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Post</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;">/post</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;"> The current user may create posts...</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Post</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div>

Via Blade Templates

When writing Blade templates, you may wish to display a portion of the page only if the user is
authorized to perform a given action. For example, you may wish to show an update form for a
blog post only if the user can actually update the post. In this situation, you may use the
@can and @cannot directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> The current user can update the post... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@elsecan</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</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: #FFCB6B;">Post</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><span style="color: #697098;"> The current user can create new posts... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@else</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> ... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endcan</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@cannot</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> The current user cannot update the post... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@elsecannot</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</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: #FFCB6B;">Post</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><span style="color: #697098;"> The current user cannot create new posts... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endcannot</span></div>

These directives are convenient shortcuts for writing @if and @unless
statements. The @can and @cannot statements above are equivalent to
the following statements:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB6B;">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;">can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> The current user can update the post... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@unless </span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB6B;">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;">can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> The current user cannot update the post... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endunless</span></div>

You may also determine if a user is authorized to perform any action from a given array of
actions. To accomplish this, use the @canany directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@canany</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view', 'delete'], $post)
<!--</span> The current user can update, view, or delete the post... -->
@elsecanany(['create'], \App\Models\Post::class)
<!--</span> The current user can create a post... -->
@endcanany

Actions That Don’t Require Models

Like most of the other authorization methods, you may pass a class name to the @can
and @cannot directives if the action does not require a model
instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@can</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</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: #FFCB6B;">Post</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><span style="color: #697098;"> The current user can create posts... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endcan</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@cannot</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">create</span><span style="color: #D9F5DD;">'</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: #FFCB6B;">Post</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><span style="color: #697098;"> The current user can't create posts... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@endcannot</span></div>

Supplying Additional
Context

When authorizing actions using policies, you may pass an array as the second argument to the
various authorization functions and helpers. The first element in the array will be used to
determine which policy should be invoked, while the rest of the array elements are passed as
parameters to the policy method and can be used for additional context when making authorization
decisions. For example, consider the following PostPolicy method definition which
contains an additional $category parameter:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Determine if the given post can be updated by the user.</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;">update</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;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$category</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #89DDFF;">->user_id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">&&</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canUpdateCategory</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$category</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

When attempting to determine if the authenticated user can update a given post, we can invoke
this policy method like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Update the given blog post.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@throws</span><span style="color: #697098;"> </span><span style="color: #697098;">\</span><span style="color: #697098;">Illuminate</span><span style="color: #697098;">\</span><span style="color: #697098;">Auth</span><span style="color: #697098;">\</span><span style="color: #697098;">Access</span><span style="color: #697098;">\</span><span style="color: #FFCB8B;">AuthorizationException</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;">update</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: #BFC7D5;">, </span><span style="color: #FFCB8B;">Post</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">RedirectResponse</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Gate</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">authorize</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">update</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->category</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;"> The current user can update the blog post...</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/posts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Authorization &
Inertia

Although authorization must always be handled on the server, it can often be convenient to
provide your frontend application with authorization data in order to properly render your
application’s UI. Laravel does not define a required convention for exposing authorization
information to an Inertia powered frontend.

However, if you are using one of Laravel’s Inertia-based starter
kits
, your application already contains a HandleInertiaRequests
middleware. Within this middleware‘s share method, you
may return shared data that will be provided to all Inertia pages in your application. This
shared data can serve as a convenient location to define authorization information for the user:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Middleware;
 
class HandleInertiaRequests extends Middleware
{
// ...
 
/**
* Define the props that are shared by default.
*
* @return array<string, mixed>
*/
public function share(Request $request)
{
return [
...parent::share($request),
'auth' => [
'user' => $request->user(),
'permissions' => [
'post' => [
'create' => $request->user()->can('create', Post::class),
],
],
],
];
}
}


Billing

Laravel Cashier (Stripe)

Introduction

Laravel Cashier Stripe provides an
expressive, fluent interface to Stripe’s subscription billing
services. It handles almost all of the boilerplate subscription billing code you are dreading
writing. In addition to basic subscription management, Cashier can handle coupons, swapping
subscription, subscription «quantities», cancellation grace periods, and even generate invoice
PDFs.

Upgrading Cashier

When upgrading to a new version of Cashier, it’s important that you carefully review
the upgrade
guide
.

exclamation

To prevent breaking changes, Cashier uses a fixed Stripe API version. Cashier 15 utilizes
Stripe API version 2023-10-16. The Stripe API version will be updated on minor
releases in order to make use of new Stripe features and improvements.

Installation

First, install the Cashier package for Stripe using the Composer package manager:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require laravel/cashier

After installing the package, publish Cashier’s migrations using the
vendor:publish Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan vendor:publish --tag="cashier-migrations"

Then, migrate your database:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan migrate

Cashier’s migrations will add several columns to your users table. They
will also create a new subscriptions table to hold all of your customer’s
subscriptions and a subscription_items table for subscriptions with multiple
prices.

If you wish, you can also publish Cashier’s configuration file using the
vendor:publish Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan vendor:publish --tag="cashier-config"

Lastly, to ensure Cashier properly handles all Stripe events, remember to configure Cashier’s webhook handling.

exclamation

Stripe recommends that any column used for storing Stripe identifiers should be
case-sensitive. Therefore, you should ensure the column collation for the
stripe_id column is set to utf8_bin when using MySQL. More
information regarding this can be found in the Stripe
documentation
.

Configuration

Billable Model

Before using Cashier, add the Billable trait to your billable model
definition. Typically, this will be the App\Models\User model. This
trait provides various methods to allow you to perform common billing tasks, such as creating
subscriptions, applying coupons, and updating payment method information:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Billable</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">Authenticatable</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Billable</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Cashier assumes your billable model will be the App\Models\User class
that ships with Laravel. If you wish to change this you may specify a different
model via the useCustomerModel method. This method should typically be
called in the boot method of your AppServiceProvider class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\Cashier\</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;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useCustomerModel</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><div class="line"><span style="color: #BFC7D5;">}</span></div>
exclamation

If you’re using a model other than Laravel’s supplied
App\Models\User model, you’ll need to publish and alter the Cashier migrations provided to match your
alternative model‘s table name.

API Keys

Next, you should configure your Stripe API keys in your application’s
.env file. You can retrieve your Stripe API keys from the Stripe control panel:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">STRIPE_KEY</span><span style="color: #BFC7D5;">=your-stripe-key</span></div><div class="line"><span style="color: #C792EA;">STRIPE_SECRET</span><span style="color: #BFC7D5;">=your-stripe-secret</span></div><div class="line"><span style="color: #C792EA;">STRIPE_WEBHOOK_SECRET</span><span style="color: #BFC7D5;">=your-stripe-webhook-secret</span></div>

exclamation

You should ensure that the STRIPE_WEBHOOK_SECRET environment
variable is defined in your application’s .env file, as this variable is used
to ensure that incoming webhooks are actually from Stripe.

Currency Configuration

The default Cashier currency is United States Dollars (USD). You can change the default currency
by setting the CASHIER_CURRENCY environment variable within your
application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_CURRENCY</span><span style="color: #BFC7D5;">=eur</span></div>

In addition to configuring Cashier’s currency, you may also specify a locale to be
used when formatting money values for display on invoices. Internally, Cashier utilizes PHP’s
NumberFormatter class
to set the currency locale:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_CURRENCY_LOCALE</span><span style="color: #BFC7D5;">=nl_BE</span></div>

exclamation

In order to use locales other than en, ensure the ext-intl PHP
extension is installed and configured on your server.

Tax Configuration

Thanks to Stripe Tax, it’s possible to automatically
calculate taxes for all invoices generated by Stripe. You can enable automatic tax calculation
by invoking the calculateTaxes method in the boot method of your
application’s App\Providers\AppServiceProvider class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">calculateTaxes</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Once tax calculation has been enabled, any new subscriptions and any one-off invoices that are
generated will receive automatic tax calculation.

For this feature to work properly, your customer’s billing details, such as the customer’s name,
address, and tax ID, need to be synced to Stripe. You may use the customer data synchronization and Tax ID methods offered by Cashier to accomplish this.

Logging

Cashier allows you to specify the log channel to be used when logging fatal Stripe errors. You
may specify the log channel by defining the CASHIER_LOGGER environment
variable within your application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_LOGGER</span><span style="color: #BFC7D5;">=stack</span></div>

Exceptions that are generated by API calls to Stripe will be logged through your application’s
default log channel.

Using Custom Models

You are free to extend the models used internally by Cashier by defining your own
model and extending the corresponding Cashier model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> CashierSubscription;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">CashierSubscription</span></div><div class="line"><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>

After defining your model, you may instruct Cashier to use your custom
model via the Laravel\Cashier\Cashier class. Typically, you should
inform Cashier about your custom models in the boot method of your
application’s App\Providers\AppServiceProvider class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\Cashier\</span><span style="color: #FFCB8B;">Subscription</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\Cashier\</span><span style="color: #FFCB8B;">SubscriptionItem</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useSubscriptionModel</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Subscription</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useSubscriptionItemModel</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">SubscriptionItem</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>

Quickstart

Selling Products

lightbulb

Before utilizing Stripe Checkout, you should define Products with fixed prices in your
Stripe dashboard. In addition, you should configure Cashier’s webhook handling.

Offering product and subscription billing via your application can be intimidating. However,
thanks to Cashier and Stripe Checkout, you
can easily build modern, robust payment integrations.

To charge customers for non-recurring, single-charge products, we’ll utilize Cashier to direct
customers to Stripe Checkout, where they will provide their payment details and confirm their
purchase. Once the payment has been made via Checkout, the customer will be redirected to a
success URL of your choosing within your application:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/checkout</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: #BEC5D4;">$stripePriceId</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_deluxe_album</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$quantity</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">([</span><span style="color: #BEC5D4;">$stripePriceId</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$quantity</span><span style="color: #BFC7D5;">], [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('checkout-success'),
'cancel_url' => route('checkout-cancel'),
]);
})->name('checkout');
 
Route::view('/checkout/success', 'checkout.success')->name('checkout-success');
Route::view('/checkout/cancel', 'checkout.cancel')->name('checkout-cancel');

As you can see in the example above, we will utilize Cashier’s provided checkout
method to redirect the customer to Stripe Checkout for a given «price identifier». When using
Stripe, «prices» refer to defined prices
for specific products
.

If necessary, the checkout method will automatically create a customer in Stripe and
connect that Stripe customer record to the corresponding user in your application’s
database. After completing the checkout session, the customer will be redirected to
a dedicated success or cancellation page where you can display an informational message to the
customer.

Providing Meta Data to Stripe Checkout

When selling products, it’s common to keep track of completed orders and purchased products via
Cart and Order models defined by your own application.
When redirecting customers to Stripe Checkout to complete a purchase, you may need to provide an
existing order identifier so that you can associate the completed purchase with the
corresponding order when the customer is redirected back to your application.

To accomplish this, you may provide an array of metadata to the
checkout method. Let’s imagine that a pending Order is created within
our application when a user begins the checkout process. Remember, the Cart and
Order models in this example are illustrative and not provided by
Cashier. You are free to implement these concepts based on the needs of your own application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Cart</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;">Order</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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/cart/{cart}/checkout</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: #BFC7D5;">, </span><span style="color: #FFCB8B;">Cart</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$cart</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">cart_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: #BEC5D4;">$cart</span><span style="color: #89DDFF;">->id</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_ids</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;">$cart</span><span style="color: #89DDFF;">->price_ids</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</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;">incomplete</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->price_ids</span><span style="color: #BFC7D5;">, [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('checkout-success').'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('checkout-cancel'),
'metadata' => ['order_id' => $order->id],
]);
})->name('checkout');

As you can see in the example above, when a user begins the checkout process, we will provide all
of the cart / order’s associated Stripe price identifiers to the checkout method.
Of course, your application is responsible for associating these items with the «shopping cart»
or order as a customer adds them. We also provide the order’s ID to the Stripe Checkout session
via the metadata array. Finally, we have added the CHECKOUT_SESSION_ID
template variable to the Checkout success route. When Stripe redirects customers
back to your application, this template variable will automatically be populated with the
Checkout session ID.

Next, let’s build the Checkout success route. This is the route that
users will be redirected to after their purchase has been completed via Stripe Checkout. Within
this route, we can retrieve the Stripe Checkout session ID and the associated
Stripe Checkout instance in order to access our provided meta data and update our customer’s
order accordingly:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Order</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;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/checkout/success</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: #BEC5D4;">$sessionId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">session_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$sessionId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">null</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></div><div class="line"><span style="color: #BFC7D5;">    }</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$session</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">stripe</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">->checkout->sessions-></span><span style="color: #82AAFF;">retrieve</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$sessionId</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$session</span><span style="color: #89DDFF;">->payment_status</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;">paid</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></div><div class="line"><span style="color: #BFC7D5;">    }</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$session</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">metadata</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">][</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">order_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: #82AAFF;">null</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findOrFail</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">update</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</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;">completed</span><span style="color: #D9F5DD;">'</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: #82AAFF;"><code>view('checkout-success', ['order' => $order]);
})->name('checkout-success');

Please refer to Stripe’s documentation for more information on the data contained by the Checkout
session object
.

Selling
Subscriptions

lightbulb

Before utilizing Stripe Checkout, you should define Products with fixed prices in your
Stripe dashboard. In addition, you should configure Cashier’s webhook handling.

Offering product and subscription billing via your application can be intimidating. However,
thanks to Cashier and Stripe Checkout, you
can easily build modern, robust payment integrations.

To learn how to sell subscriptions using Cashier and Stripe Checkout, let’s consider the simple
scenario of a subscription service with a basic monthly (price_basic_monthly) and
yearly (price_basic_yearly) plan. These two prices could be grouped under a «Basic»
product (pro_basic) in our Stripe dashboard. In addition, our subscription service
might offer an Expert plan as pro_expert.

First, let’s discover how a customer can subscribe to our services. Of course, you can imagine
the customer might click a «subscribe» button for the Basic plan on our application’s pricing
page. This button or link should direct the user to a Laravel route which creates
the Stripe Checkout session for their chosen plan:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialDays</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: #89DDFF;">-></span><span style="color: #82AAFF;">allowPromotionCodes</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('your-success-route'),
'cancel_url' => route('your-cancel-route'),
]);
});

As you can see in the example above, we will redirect the customer to a Stripe Checkout session
which will allow them to subscribe to our Basic plan. After a successful checkout or
cancellation, the customer will be redirected back to the URL we provided to the
checkout method. To know when their subscription has actually started (since some
payment methods require a few seconds to process), we’ll also need to configure Cashier’s webhook handling.

Now that customers can start subscriptions, we need to restrict certain portions of our
application so that only subscribed users can access them. Of course, we can always determine a
user’s current subscription status via the subscribed method provided by Cashier’s
Billable trait:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribed</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

We can even easily determine if a user is subscribed to specific product or price:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToProduct</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pro_basic</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed to our Basic product.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed to our monthly Basic plan.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

Building a Subscribed Middleware

For convenience, you may wish to create a middleware
which determines if the incoming request is from a subscribed user. Once this
middleware has been defined, you may easily assign it to a route to
prevent users that are not subscribed from accessing the route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class Subscribed
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
if (! $request->user()?->subscribed()) {
// Redirect user to billing page and ask them to subscribe...
return redirect('/billing');
}
 
return $next($request);
}
}

Once the middleware has been defined, you may assign it to a route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Http\Middleware\</span><span style="color: #FFCB8B;">Subscribed</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/dashboard</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware([Subscribed::class]);

Allowing Customers to
Manage Their Billing Plan

Of course, customers may want to change their subscription plan to another product or «tier». The
easiest way to allow this is by directing customers to Stripe’s Customer Billing Portal, which
provides a hosted user interface that allows customers to download invoices, update their
payment method, and change subscription plans.

First, define a link or button within your application that directs users to a Laravel
route which we will utilize to initiate a Billing Portal session:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">a</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">href</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">{{</span><span style="color: #C3E88D;"> </span><span style="color: #82AAFF;"><code>route('billing') }}">
Billing
</spana>

Next, let’s define the route that initiates a Stripe Customer Billing Portal session
and redirects the user to the Portal. The redirectToBillingPortal method accepts
the URL that users should be returned to when exiting the Portal:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/billing</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">redirectToBillingPortal</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
})->middleware(['auth'])->name('billing');
lightbulb

As long as you have configured Cashier’s webhook handling, Cashier will
automatically keep your application’s Cashier-related database tables in sync
by inspecting the incoming webhooks from Stripe. So, for example, when a user cancels their
subscription via Stripe’s Customer Billing Portal, Cashier will receive the corresponding
webhook and mark the subscription as «canceled» in your application’s database.

Customers

Retrieving Customers

You can retrieve a customer by their Stripe ID using the Cashier::findBillable
method. This method will return an instance of the billable model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findBillable</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$stripeId</span><span style="color: #BFC7D5;">);</span></div>

Creating Customers

Occasionally, you may wish to create a Stripe customer without beginning a subscription. You may
accomplish this using the createAsStripeCustomer method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeCustomer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAsStripeCustomer</span><span style="color: #BFC7D5;">();</span></div>

Once the customer has been created in Stripe, you may begin a subscription at a later date. You
may provide an optional $options array to pass in any additional customer creation parameters that are
supported by the Stripe API
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeCustomer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAsStripeCustomer</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$options</span><span style="color: #BFC7D5;">);</span></div>

You may use the asStripeCustomer method if you want to return the Stripe customer
object for a billable model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeCustomer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">asStripeCustomer</span><span style="color: #BFC7D5;">();</span></div>

The createOrGetStripeCustomer method may be used if you would like to retrieve the
Stripe customer object for a given billable model but are not sure whether the
billable model is already a customer within Stripe. This method will create a new
customer in Stripe if one does not already exist:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeCustomer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createOrGetStripeCustomer</span><span style="color: #BFC7D5;">();</span></div>

Updating Customers

Occasionally, you may wish to update the Stripe customer directly with additional information.
You may accomplish this using the updateStripeCustomer method. This method accepts
an array of customer update options
supported by the Stripe API
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeCustomer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateStripeCustomer</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$options</span><span style="color: #BFC7D5;">);</span></div>

Balances

Stripe allows you to credit or debit a customer’s «balance». Later, this balance will be credited
or debited on new invoices. To check the customer’s total balance you may use the
balance method that is available on your billable model. The
balance method will return a formatted string representation of the balance in the
customer’s currency:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$balance</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">balance</span><span style="color: #BFC7D5;">();</span></div>

To credit a customer’s balance, you may provide a value to the creditBalance method.
If you wish, you may also provide a description:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">creditBalance</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Premium customer top-up.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Providing a value to the debitBalance method will debit the customer’s balance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">debitBalance</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;">Bad usage penalty.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

The applyBalance method will create new customer balance transactions for the
customer. You may retrieve these transaction records using the balanceTransactions
method, which may be useful in order to provide a log of credits and debits for the customer to
review:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve all transactions...</span></div><div class="line"><span style="color: #BEC5D4;">$transactions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">balanceTransactions</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$transactions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #BFC7D5;">) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Transaction amount...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$amount</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">amount</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> $2.31</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve the related invoice when available...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoice</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Tax IDs

Cashier offers an easy way to manage a customer’s tax IDs. For example, the taxIds
method may be used to retrieve all of the tax IDs that are assigned to
a customer as a collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$taxIds</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">taxIds</span><span style="color: #BFC7D5;">();</span></div>

You can also retrieve a specific tax ID for a customer by its identifier:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$taxId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findTaxId</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">txi_belgium</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may create a new Tax ID by providing a valid type and
value to the createTaxId method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$taxId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createTaxId</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eu_vat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">BE0123456789</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

The createTaxId method will immediately add the VAT ID to the customer’s account. Verification of VAT IDs
is also done by Stripe
; however, this is an asynchronous process. You can be notified of
verification updates by subscribing to the customer.tax_id.updated webhook event
and inspecting the
VAT IDs verification parameter
. For more information on handling webhooks,
please consult the documentation on defining webhook
handlers
.

You may delete a tax ID using the deleteTaxId method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">deleteTaxId</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">txi_belgium</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Syncing
Customer Data With Stripe

Typically, when your application’s users update their name, email address, or other information
that is also stored by Stripe, you should inform Stripe of the updates. By doing so, Stripe’s
copy of the information will be in sync with your application’s.

To automate this, you may define an event listener on your billable model that
reacts to the model‘s updated event. Then, within your
event listener, you may invoke the syncStripeCustomerDetails method on the
model:

<!-- Syntax highlighted by torchlight.dev --><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;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> Illuminate\Events\</span><span style="color: #FFCB8B;">queueable</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;"> * The "booted" method of the <code>model.
*/
protected static function booted(): void
{
static::updated(queueable(function (User $customer) {
if ($customer->hasStripeId()) {
$customer->syncStripeCustomerDetails();
}
}));
}

Now, every time your customer model is updated, its information will be synced with
Stripe. For convenience, Cashier will automatically sync your customer’s information with Stripe
on the initial creation of the customer.

You may customize the columns used for syncing customer information to Stripe by overriding a
variety of methods provided by Cashier. For example, you may override the
stripeName method to customize the attribute that should be considered the
customer’s «name» when Cashier syncs customer information to Stripe:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the customer name that should be synced to Stripe.</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;">stripeName</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;">|</span><span style="color: #C792EA;">null</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: #FF5572;">$this</span><span style="color: #89DDFF;">->company_name</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Similarly, you may override the stripeEmail, stripePhone,
stripeAddress, and stripePreferredLocales methods. These methods will
sync information to their corresponding customer parameters when updating the Stripe customer object.
If you wish to take total control over the customer information sync process, you may override
the syncStripeCustomerDetails method.

Billing Portal

Stripe offers an easy way
to set up a billing portal
so that your customer can manage their subscription, payment
methods, and view their billing history. You can redirect your users to the billing
portal by invoking the redirectToBillingPortal method on the billable
model from a controller or route:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/billing-portal</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">redirectToBillingPortal</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

By default, when the user is finished managing their subscription, they will be able to return to
the home route of your application via a link within the Stripe
billing portal. You may provide a custom URL that the user should return to by passing the URL
as an argument to the redirectToBillingPortal method:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/billing-portal</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">redirectToBillingPortal</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('billing'));
});

If you would like to generate the URL to the billing portal without generating an HTTP redirect
response, you may invoke the billingPortalUrl method:

<!-- 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: #BEC5D4;">$request</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;">billingPortalUrl</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('billing'));

Payment Methods

Storing Payment Methods

In order to create subscriptions or perform «one-off» charges with Stripe, you will need to store
a payment method and retrieve its identifier from Stripe. The approach used to accomplish this
differs based on whether you plan to use the payment method for subscriptions or single charges,
so we will examine both below.

Payment
Methods for Subscriptions

When storing a customer’s credit card information for future use by a subscription, the Stripe
«Setup Intents» API must be used to securely gather the customer’s payment method details. A
«Setup Intent» indicates to Stripe the intention to charge a customer’s payment method.
Cashier’s Billable trait includes the createSetupIntent method to
easily create a new Setup Intent. You should invoke this method from the route or
controller that will render the form which gathers your customer’s payment method
details:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;"><code>view('update-payment-method', [
'intent' => $user->createSetupIntent()
]);

After you have created the Setup Intent and passed it to the view, you should attach
its secret to the element that will gather the payment method. For example, consider this
«update payment method» form:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">input</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-holder-name</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">text</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Stripe Elements Placeholder </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-element</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-button</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">data-secret</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">{{ $intent->client_secret }}</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Update Payment Method</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

Next, the Stripe.js library may be used to attach a Stripe Element to the form and securely gather
the customer’s payment details:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">src</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">https://js.stripe.com/v3/</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">stripe</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Stripe</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">stripe-public-key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">elements</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> stripe</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">elements</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardElement</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> elements</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">    cardElement</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">mount</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">#card-element</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div>

Next, the card can be verified and a secure «payment method identifier» can be retrieved from
Stripe using Stripe’s
confirmCardSetup method
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardHolderName</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> document</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">getElementById</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card-holder-name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardButton</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> document</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">getElementById</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card-button</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">clientSecret</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> cardButton</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">dataset</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">secret</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">cardButton</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">addEventListener</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">click</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">async</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</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: #C792EA;">const</span><span style="color: #BFC7D5;"> { </span><span style="color: #82AAFF;">setupIntent</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">error</span><span style="color: #BFC7D5;"> } </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">await</span><span style="color: #BFC7D5;"> stripe</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">confirmCardSetup</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;">        clientSecret, {</span></div><div class="line"><span style="color: #BFC7D5;">            payment_method: {</span></div><div class="line"><span style="color: #BFC7D5;">                card: cardElement,</span></div><div class="line"><span style="color: #BFC7D5;">                billing_details: { name: cardHolderName</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">value</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></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (error) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Display "error.message" to the user...</span></div><div class="line"><span style="color: #BFC7D5;">    } </span><span style="color: #C792EA;">else</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The card has been verified successfully...</span></div><div class="line"><span style="color: #BFC7D5;">    }</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

After the card has been verified by Stripe, you may pass the resulting
setupIntent.payment_method identifier to your Laravel application, where it can be
attached to the customer. The payment method can either be added as a new payment method or used to update the default payment method.
You can also immediately use the payment method identifier to create a new subscription.

lightbulb

If you would like more information about Setup Intents and gathering customer payment
details please review this
overview provided by Stripe
.

Payment
Methods for Single Charges

Of course, when making a single charge against a customer’s payment method, we will only need to
use a payment method identifier once. Due to Stripe limitations, you may not use the stored
default payment method of a customer for single charges. You must allow the customer to enter
their payment method details using the Stripe.js library. For example, consider the following
form:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">input</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-holder-name</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">text</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Stripe Elements Placeholder </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-element</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">card-button</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Process Payment</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

After defining such a form, the Stripe.js library may be used to attach a Stripe Element to the form and securely gather
the customer’s payment details:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">src</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">https://js.stripe.com/v3/</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">stripe</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Stripe</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">stripe-public-key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">elements</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> stripe</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">elements</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardElement</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> elements</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">    cardElement</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">mount</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">#card-element</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div>

Next, the card can be verified and a secure «payment method identifier» can be retrieved from
Stripe using Stripe’s
createPaymentMethod method
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardHolderName</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> document</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">getElementById</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card-holder-name</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #C792EA;">const</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">cardButton</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> document</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">getElementById</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card-button</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">cardButton</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">addEventListener</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">click</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">async</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</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: #C792EA;">const</span><span style="color: #BFC7D5;"> { </span><span style="color: #82AAFF;">paymentMethod</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">error</span><span style="color: #BFC7D5;"> } </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">await</span><span style="color: #BFC7D5;"> stripe</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">createPaymentMethod</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, cardElement, {</span></div><div class="line"><span style="color: #BFC7D5;">            billing_details: { name: cardHolderName</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">value</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: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (error) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Display "error.message" to the user...</span></div><div class="line"><span style="color: #BFC7D5;">    } </span><span style="color: #C792EA;">else</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> The card has been verified successfully...</span></div><div class="line"><span style="color: #BFC7D5;">    }</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

If the card is verified successfully, you may pass the paymentMethod.id to your
Laravel application and process a single charge.

Retrieving Payment
Methods

The paymentMethods method on the billable model instance returns a
collection of Laravel\Cashier\PaymentMethod instances:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$paymentMethods</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">paymentMethods</span><span style="color: #BFC7D5;">();</span></div>

By default, this method will return payment methods of every type. To retrieve payment methods of
a specific type, you may pass the type as an argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$paymentMethods</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">paymentMethods</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">sepa_debit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

To retrieve the customer’s default payment method, the defaultPaymentMethod method
may be used:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">defaultPaymentMethod</span><span style="color: #BFC7D5;">();</span></div>

You can retrieve a specific payment method that is attached to the billable model
using the findPaymentMethod method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findPaymentMethod</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethodId</span><span style="color: #BFC7D5;">);</span></div>

Payment Method Presence

To determine if a billable model has a default payment method attached to their
account, invoke the hasDefaultPaymentMethod method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasDefaultPaymentMethod</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>

You may use the hasPaymentMethod method to determine if a billable
model has at least one payment method attached to their account:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasPaymentMethod</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>

This method will determine if the billable model has any payment method at all. To
determine if a payment method of a specific type exists for the model, you may pass
the type as an argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasPaymentMethod</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">sepa_debit</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>

Updating
the Default Payment Method

The updateDefaultPaymentMethod method may be used to update a customer’s default
payment method information. This method accepts a Stripe payment method identifier and will
assign the new payment method as the default billing payment method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateDefaultPaymentMethod</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

To sync your default payment method information with the customer’s default payment method
information in Stripe, you may use the updateDefaultPaymentMethodFromStripe method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateDefaultPaymentMethodFromStripe</span><span style="color: #BFC7D5;">();</span></div>
exclamation

The default payment method on a customer can only be used for invoicing and creating new
subscriptions. Due to limitations imposed by Stripe, it may not be used for single charges.

Adding Payment Methods

To add a new payment method, you may call the addPaymentMethod method on the
billable model, passing the payment method identifier:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addPaymentMethod</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>
lightbulb

To learn how to retrieve payment method identifiers please review the payment method storage documentation.

Deleting Payment Methods

To delete a payment method, you may call the delete method on the
Laravel\Cashier\PaymentMethod instance you wish to delete:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">delete</span><span style="color: #BFC7D5;">();</span></div>

The deletePaymentMethod method will delete a specific payment method from the
billable model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">deletePaymentMethod</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pm_visa</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

The deletePaymentMethods method will delete all of the payment method information
for the billable model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">deletePaymentMethods</span><span style="color: #BFC7D5;">();</span></div>

By default, this method will delete payment methods of every type. To delete payment methods of a
specific type you can pass the type as an argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">deletePaymentMethods</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">sepa_debit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
exclamation

If a user has an active subscription, your application should not allow them to delete their
default payment method.

Subscriptions

Subscriptions provide a way to set up recurring payments for your customers. Stripe subscriptions
managed by Cashier provide support for multiple subscription prices, subscription quantities,
trials, and more.

Creating Subscriptions

To create a subscription, first retrieve an instance of your billable model, which
typically will be an instance of App\Models\User. Once you have retrieved the
model instance, you may use the newSubscription method to create the
model‘s subscription:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">    )</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

The first argument passed to the newSubscription method should be the internal type
of the subscription. If your application only offers a single subscription, you might call this
default or primary. This subscription type is only for internal
application usage and is not meant to be shown to users. In addition, it should not contain
spaces and it should never be changed after creating the subscription. The second argument is
the specific price the user is subscribing to. This value should correspond to the price’s
identifier in Stripe.

The create method, which accepts a Stripe payment
method identifier
or Stripe PaymentMethod object, will begin the
subscription as well as update your database with the billable model‘s
Stripe customer ID and other relevant billing information.

exclamation

Passing a payment method identifier directly to the create subscription method
will also automatically add it to the user’s stored payment methods.

Collecting Recurring Payments via
Invoice Emails

Instead of collecting a customer’s recurring payments automatically, you may instruct Stripe to
email an invoice to the customer each time their recurring payment is due. Then, the customer
may manually pay the invoice once they receive it. The customer does not need to provide a
payment method up front when collecting recurring payments via invoices:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAndSendInvoice</span><span style="color: #BFC7D5;">();</span></div>

The amount of time a customer has to pay their invoice before their subscription is canceled is
determined by the days_until_due option. By default, this is 30 days; however, you
may provide a specific value for this option if you wish:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAndSendInvoice</span><span style="color: #BFC7D5;">([], [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">days_until_due</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;">30</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Quantities

If you would like to set a specific quantity for the price
when creating the subscription, you should invoke the quantity method on the
subscription builder before creating the subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">quantity</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: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

Additional Details

If you would like to specify additional customer or subscription options supported
by Stripe, you may do so by passing them as the second and third arguments to the
create method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</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><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">metadata</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;">note</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;">Some extra information.</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Coupons

If you would like to apply a coupon when creating the subscription, you may use the
withCoupon method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withCoupon</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">code</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

Or, if you would like to apply a Stripe promotion
code
, you may use the withPromotionCode method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">promo_code_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

The given promotion code ID should be the Stripe API ID assigned to the promotion code and not
the customer facing promotion code. If you need to find a promotion code ID based on a given
customer facing promotion code, you may use the findPromotionCode method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Find a promotion code ID by its customer facing code...</span></div><div class="line"><span style="color: #BEC5D4;">$promotionCode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">SUMMERSALE</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;"> Find an active promotion code ID by its customer facing code...</span></div><div class="line"><span style="color: #BEC5D4;">$promotionCode</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findActivePromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">SUMMERSALE</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

In the example above, the returned $promotionCode object is an instance of
Laravel\Cashier\PromotionCode. This class decorates an underlying
Stripe\PromotionCode object. You can retrieve the coupon related to the promotion
code by invoking the coupon method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$coupon</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">SUMMERSALE</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">coupon</span><span style="color: #BFC7D5;">();</span></div>

The coupon instance allows you to determine the discount amount and whether the coupon represents
a fixed discount or percentage based discount:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$coupon</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isPercentage</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;">$coupon</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">percentOff</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">.</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">%</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">; </span><span style="color: #697098;">//</span><span style="color: #697098;"> 21.5%</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">else</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;">$coupon</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">amountOff</span><span style="color: #BFC7D5;">(); </span><span style="color: #697098;">//</span><span style="color: #697098;"> $5.99</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

You can also retrieve the discounts that are currently applied to a customer or subscription:

<!-- 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: #BEC5D4;">$billable</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">discount</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: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">discount</span><span style="color: #BFC7D5;">();</span></div>

The returned Laravel\Cashier\Discount instances decorate an underlying
Stripe\Discount object instance. You may retrieve the coupon related to this
discount by invoking the coupon method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$coupon</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">discount</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">coupon</span><span style="color: #BFC7D5;">();</span></div>

If you would like to apply a new coupon or promotion code to a customer or subscription, you may
do so via the applyCoupon or applyPromotionCode methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$billable</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">applyCoupon</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">coupon_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BEC5D4;">$billable</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">applyPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">promotion_code_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">applyCoupon</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">coupon_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">applyPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">promotion_code_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Remember, you should use the Stripe API ID assigned to the promotion code and not the customer
facing promotion code. Only one coupon or promotion code can be applied to a customer or
subscription at a given time.

For more info on this subject, please consult the Stripe documentation regarding coupons and promotion codes.

Adding Subscriptions

If you would like to add a subscription to a customer who already has a default payment method
you may invoke the add method on the subscription builder:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">add</span><span style="color: #BFC7D5;">();</span></div>

Creating Subscriptions From the
Stripe Dashboard

You may also create subscriptions from the Stripe dashboard itself. When doing so, Cashier will
sync newly added subscriptions and assign them a type of default. To customize the
subscription type that is assigned to dashboard created subscriptions, define webhook event handlers.

In addition, you may only create one type of subscription via the Stripe dashboard. If your
application offers multiple subscriptions that use different types, only one type of
subscription may be added through the Stripe dashboard.

Finally, you should always make sure to only add one active subscription per type of subscription
offered by your application. If a customer has two default subscriptions, only the
most recently added subscription will be used by Cashier even though both would be synced with
your application’s database.

Checking Subscription
Status

Once a customer is subscribed to your application, you may easily check their subscription status
using a variety of convenient methods. First, the subscribed method returns
true if the customer has an active subscription, even if the subscription is
currently within its trial period. The subscribed method accepts the type of the
subscription as its first argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribed</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

The subscribed method also makes a great candidate for a route middleware, allowing you to
filter access to routes and controllers based on the user’s
subscription status:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class EnsureUserIsSubscribed
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($request->user() && ! $request->user()->subscribed('default')) {
// This user is not a paying customer...
return redirect('/billing');
}
 
return $next($request);
}
}

If you would like to determine if a user is still within their trial period, you may use the
onTrial method. This method can be useful for determining if you should display a
warning to the user that they are still on their trial period:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</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>

The subscribedToProduct method may be used to determine if the user is subscribed to
a given product based on a given Stripe product’s identifier. In Stripe, products are
collections of prices. In this example, we will determine if the user’s default
subscription is actively subscribed to the application’s «premium» product. The given Stripe
product identifier should correspond to one of your product’s identifiers in the Stripe
dashboard:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToProduct</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod_premium</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

By passing an array to the subscribedToProduct method, you may determine if the
user’s default subscription is actively subscribed to the application’s «basic» or
«premium» product:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToProduct</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod_basic</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod_premium</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">], </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

The subscribedToPrice method may be used to determine if a customer’s subscription
corresponds to a given price ID:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

The recurring method may be used to determine if the user is currently subscribed
and is no longer within their trial period:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">recurring</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>
exclamation

If a user has two subscriptions with the same type, the most recent subscription will always
be returned by the subscription method. For example, a user might have two
subscription records with the type of default; however, one of the
subscriptions may be an old, expired subscription, while the other is the current, active
subscription. The most recent subscription will always be returned while older subscriptions
are kept in the database for historical review.

Canceled
Subscription Status

To determine if the user was once an active subscriber but has canceled their subscription, you
may use the canceled method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</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>

You may also determine if a user has canceled their subscription but are still on their «grace
period» until the subscription fully expires. For example, if a user cancels a subscription on
March 5th that was originally scheduled to expire on March 10th, the user is on their «grace
period» until March 10th. Note that the subscribed method still returns
true during this time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</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>

To determine if the user has canceled their subscription and is no longer within their «grace
period», you may use the ended method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ended</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>

Incomplete and
Past Due Status

If a subscription requires a secondary payment action after creation the subscription will be
marked as incomplete. Subscription statuses are stored in the
stripe_status column of Cashier’s subscriptions database
table.

Similarly, if a secondary payment action is required when swapping prices the subscription will
be marked as past_due. When your subscription is in either of these states it will
not be active until the customer has confirmed their payment. Determining if a subscription has
an incomplete payment may be accomplished using the hasIncompletePayment method on
the billable model or a subscription instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasIncompletePayment</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasIncompletePayment</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>

When a subscription has an incomplete payment, you should direct the user to Cashier’s payment
confirmation page, passing the latestPayment identifier. You may use the
latestPayment method available on subscription instance to retrieve this
identifier:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">a</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">href</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">{{ <code>route('cashier.payment', $subscription->latestPayment()->id) }}">
Please confirm your payment.
</spana>

If you would like the subscription to still be considered active when it’s in a
past_due or incomplete state, you may use the
keepPastDueSubscriptionsActive and keepIncompleteSubscriptionsActive
methods provided by Cashier. Typically, these methods should be called in the
register method of your App\Providers\AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</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;"> * Register 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;">register</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">keepPastDueSubscriptionsActive</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">keepIncompleteSubscriptionsActive</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
exclamation

When a subscription is in an incomplete state it cannot be changed until the
payment is confirmed. Therefore, the swap and updateQuantity
methods will throw an exception when the subscription is in an incomplete
state.

Subscription Scopes

Most subscription states are also available as query scopes so that you may easily query your
database for subscriptions that are in a given state:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Get all active subscriptions...</span></div><div class="line"><span style="color: #BEC5D4;">$subscriptions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">active</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Get all of the canceled subscriptions for a user...</span></div><div class="line"><span style="color: #BEC5D4;">$subscriptions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscriptions</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div>

A complete list of available scopes is available below:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">active</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ended</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incomplete</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notCanceled</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notOnGracePeriod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notOnTrial</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pastDue</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">recurring</span><span style="color: #BFC7D5;">();</span></div>

Changing Prices

After a customer is subscribed to your application, they may occasionally want to change to a new
subscription price. To swap a customer to a new price, pass the Stripe price’s identifier to the
swap method. When swapping prices, it is assumed that the user would like to
re-activate their subscription if it was previously canceled. The given price identifier should
correspond to a Stripe price identifier available in the Stripe dashboard:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> App\Models\</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_yearly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

If the customer is on trial, the trial period will be maintained. Additionally, if a «quantity»
exists for the subscription, that quantity will also be maintained.

If you would like to swap prices and cancel any trial period the customer is currently on, you
may invoke the skipTrial method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipTrial</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_yearly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

If you would like to swap prices and immediately invoice the customer instead of waiting for
their next billing cycle, you may use the swapAndInvoice method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swapAndInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_yearly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Prorations

By default, Stripe prorates charges when swapping between prices. The noProrate
method may be used to update the subscription’s price without prorating the charges:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_yearly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

For more information on subscription proration, consult the Stripe documentation.

exclamation

Executing the noProrate method before the swapAndInvoice method
will have no effect on proration. An invoice will always be issued.

Subscription Quantity

Sometimes subscriptions are affected by «quantity». For example, a project management application
might charge $10 per month per project. You may use the incrementQuantity and
decrementQuantity methods to easily increment or decrement your subscription
quantity:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Add five to the subscription's current quantity...</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</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;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">decrementQuantity</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Subtract five from the subscription's current quantity...</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">decrementQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div>

Alternatively, you may set a specific quantity using the updateQuantity method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div>

The noProrate method may be used to update the subscription’s quantity without
prorating the charges:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div>

For more information on subscription quantities, consult the Stripe documentation.

Quantities for Subscriptions With
Multiple Products

If your subscription is a subscription with
multiple products
, you should pass the ID of the price whose quantity you wish to
increment or decrement as the second argument to the increment / decrement methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</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;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Subscriptions With Multiple Products

Subscription with
multiple products
allow you to assign multiple billing products to a single
subscription. For example, imagine you are building a customer service «helpdesk» application
that has a base subscription price of $10 per month but offers a live chat add-on product for an
additional $15 per month. Information for subscriptions with multiple products is stored in
Cashier’s subscription_items database table.

You may specify multiple products for a given subscription by passing an array of prices as the
second argument to the newSubscription method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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_monthly</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_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">    ])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

In the example above, the customer will have two prices attached to their default
subscription. Both prices will be charged on their respective billing intervals. If necessary,
you may use the quantity method to indicate a specific quantity for each price:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">quantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

If you would like to add another price to an existing subscription, you may invoke the
subscription’s addPrice method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

The example above will add the new price and the customer will be billed for it on their next
billing cycle. If you would like to bill the customer immediately you may use the
addPriceAndInvoice method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addPriceAndInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

If you would like to add a price with a specific quantity, you can pass the quantity as the
second argument of the addPrice or addPriceAndInvoice methods:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div>

You may remove prices from subscriptions using the removePrice method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">removePrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
exclamation

You may not remove the last price on a subscription. Instead, you should simply cancel the
subscription.

Swapping Prices

You may also change the prices attached to a subscription with multiple products. For example,
imagine a customer has a price_basic subscription with a price_chat
add-on product and you want to upgrade the customer from the price_basic to the
price_pro price:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_pro</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div>

When executing the example above, the underlying subscription item with the
price_basic is deleted and the one with the price_chat is preserved.
Additionally, a new subscription item for the price_pro is created.

You can also specify subscription item options by passing an array of key / value pairs to the
swap method. For example, you may need to specify the subscription price
quantities:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</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_pro</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;">quantity</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;">5</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_chat</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

If you want to swap a single price on a subscription, you may do so using the swap
method on the subscription item itself. This approach is particularly useful if you would like
to preserve all of the existing metadata on the subscription’s other prices:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findItemOrFail</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_pro</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Proration

By default, Stripe will prorate charges when adding or removing prices from a subscription with
multiple products. If you would like to make a price adjustment without proration, you should
chain the noProrate method onto your price operation:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">removePrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Quantities

If you would like to update quantities on individual subscription prices, you may do so using the
existing quantity methods by passing the ID of the price as
an additional argument to the method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">decrementQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
exclamation

When a subscription has multiple prices the stripe_price and
quantity attributes on the Subscription model will be
null. To access the individual price attributes, you should use the
items relationship available on the Subscription
model.

Subscription Items

When a subscription has multiple prices, it will have multiple subscription «items» stored in
your database‘s subscription_items table. You may access these via the
items relationship on the subscription:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subscriptionItem</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">->items-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Retrieve the Stripe price and quantity for a specific item...</span></div><div class="line"><span style="color: #BEC5D4;">$stripePrice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscriptionItem</span><span style="color: #89DDFF;">->stripe_price</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BEC5D4;">$quantity</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscriptionItem</span><span style="color: #89DDFF;">->quantity</span><span style="color: #BFC7D5;">;</span></div>

You can also retrieve a specific price using the findItemOrFail method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subscriptionItem</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findItemOrFail</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Multiple Subscriptions

Stripe allows your customers to have multiple subscriptions simultaneously. For example, you may
run a gym that offers a swimming subscription and a weight-lifting subscription, and each
subscription may have different pricing. Of course, customers should be able to subscribe to
either or both plans.

When your application creates subscriptions, you may provide the type of the subscription to the
newSubscription method. The type may be any string that represents the type of
subscription the user is initiating:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/swimming/subscribe</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: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">price</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_swimming_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

In this example, we initiated a monthly swimming subscription for the customer. However, they may
want to swap to a yearly subscription at a later time. When adjusting the customer’s
subscription, we can simply swap the price on the swimming subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_swimming_yearly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Of course, you may also cancel the subscription entirely:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancel</span><span style="color: #BFC7D5;">();</span></div>

Metered Billing

Metered billing
allows you to charge customers based on their product usage during a billing cycle. For example,
you may charge customers based on the number of text messages or emails they send per month.

To start using metered billing, you will first need to create a new product in your Stripe
dashboard with a metered price. Then, use the meteredPrice to add the metered price
ID to a customer subscription:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">meteredPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_metered</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

You may also start a metered subscription via Stripe Checkout:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [])</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">meteredPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_metered</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</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;"><code>view('your-checkout-view', [
'checkout' => $checkout,
]);

Reporting Usage

As your customer uses your application, you will report their usage to Stripe so that they can be
billed accurately. To increment the usage of a metered subscription, you may use the
reportUsage method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reportUsage</span><span style="color: #BFC7D5;">();</span></div>

By default, a «usage quantity» of 1 is added to the billing period. Alternatively, you may pass a
specific amount of «usage» to add to the customer’s usage for the billing period:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reportUsage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">15</span><span style="color: #BFC7D5;">);</span></div>

If your application offers multiple prices on a single subscription, you will need to use the
reportUsageFor method to specify the metered price you want to report usage for:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reportUsageFor</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_metered</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">15</span><span style="color: #BFC7D5;">);</span></div>

Sometimes, you may need to update usage which you have previously reported. To accomplish this,
you may pass a timestamp or a DateTimeInterface instance as the second parameter to
reportUsage. When doing so, Stripe will update the usage that was reported at that
given time. You can continue to update previous usage records as the given date and time is
still within the current billing period:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reportUsage</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$timestamp</span><span style="color: #BFC7D5;">);</span></div>

Retrieving Usage Records

To retrieve a customer’s past usage, you may use a subscription instance’s
usageRecords method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$usageRecords</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">usageRecords</span><span style="color: #BFC7D5;">();</span></div>

If your application offers multiple prices on a single subscription, you may use the
usageRecordsFor method to specify the metered price that you wish to retrieve usage
records for:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$usageRecords</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">usageRecordsFor</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_metered</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

The usageRecords and usageRecordsFor methods return a Collection
instance containing an associative array of usage records. You may iterate over this array to
display a customer’s total usage:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">@</span><span style="color: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$usageRecords</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$usageRecord</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Period</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Starting</span><span style="color: #BFC7D5;">: {{ </span><span style="color: #BEC5D4;">$usageRecord</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">period</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">][</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">start</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] }}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Period</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Ending</span><span style="color: #BFC7D5;">: {{ </span><span style="color: #BEC5D4;">$usageRecord</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">period</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">][</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">end</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] }}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Total</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">Usage</span><span style="color: #BFC7D5;">: {{ </span><span style="color: #BEC5D4;">$usageRecord</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">total_usage</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">] }}</span></div><div class="line"><span style="color: #89DDFF;">@</span><span style="color: #C792EA;">endforeach</span></div>

For a full reference of all usage data returned and how to use Stripe’s cursor based pagination,
please consult the official
Stripe API documentation
.

Subscription Taxes

exclamation

Instead of calculating Tax Rates manually, you can automatically calculate taxes using Stripe Tax

To specify the tax rates a user pays on a subscription, you should implement the
taxRates method on your billable model and return an array containing
the Stripe tax rate IDs. You can define these tax rates in your Stripe dashboard:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The tax rates that should apply to the customer's subscriptions.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">int</span><span style="color: #697098;">, string></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;">taxRates</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: #D9F5DD;">'</span><span style="color: #C3E88D;">txr_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

The taxRates method enables you to apply a tax rate on a customer-by-customer basis,
which may be helpful for a user base that spans multiple countries and tax rates.

If you’re offering subscriptions with multiple products, you may define different tax rates for
each price by implementing a priceTaxRates method on your billable
model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The tax rates that should apply to the customer's subscriptions.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">string</span><span style="color: #697098;">, array<int, string="">></int,></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;">priceTaxRates</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</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;">txr_id</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>
exclamation

The taxRates method only applies to subscription charges. If you use Cashier to
make «one-off» charges, you will need to manually specify the tax rate at that time.

Syncing Tax Rates

When changing the hard-coded tax rate IDs returned by the taxRates method, the tax
settings on any existing subscriptions for the user will remain the same. If you wish to update
the tax value for existing subscriptions with the new taxRates values, you should
call the syncTaxRates method on the user’s subscription instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">syncTaxRates</span><span style="color: #BFC7D5;">();</span></div>

This will also sync any item tax rates for a subscription with multiple products. If your
application is offering subscriptions with multiple products, you should ensure that your
billable model implements the priceTaxRates method discussed above.

Tax Exemption

Cashier also offers the isNotTaxExempt, isTaxExempt, and
reverseChargeApplies methods to determine if the customer is tax exempt. These
methods will call the Stripe API to determine a customer’s tax exemption status:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isTaxExempt</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotTaxExempt</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reverseChargeApplies</span><span style="color: #BFC7D5;">();</span></div>
exclamation

These methods are also available on any Laravel\Cashier\Invoice object.
However, when invoked on an Invoice object, the methods will determine the
exemption status at the time the invoice was created.

Subscription Anchor Date

By default, the billing cycle anchor is the date the subscription was created or, if a trial
period is used, the date that the trial ends. If you would like to modify the billing anchor
date, you may use the anchorBillingCycleOn method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$anchor</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;">parse</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first day of next month</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">anchorBillingCycleOn</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$anchor</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">startOfDay</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

For more information on managing subscription billing cycles, consult the Stripe billing cycle
documentation

Cancelling Subscriptions

To cancel a subscription, call the cancel method on the user’s subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancel</span><span style="color: #BFC7D5;">();</span></div>

When a subscription is canceled, Cashier will automatically set the ends_at column
in your subscriptions database table. This column is used to know when
the subscribed method should begin returning false.

For example, if a customer cancels a subscription on March 1st, but the subscription was not
scheduled to end until March 5th, the subscribed method will continue to return
true until March 5th. This is done because a user is typically allowed to continue
using an application until the end of their billing cycle.

You may determine if a user has canceled their subscription but are still on their «grace period»
using the onGracePeriod method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</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>

If you wish to cancel a subscription immediately, call the cancelNow method on the
user’s subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancelNow</span><span style="color: #BFC7D5;">();</span></div>

If you wish to cancel a subscription immediately and invoice any remaining un-invoiced metered
usage or new / pending proration invoice items, call the cancelNowAndInvoice method
on the user’s subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancelNowAndInvoice</span><span style="color: #BFC7D5;">();</span></div>

You may also choose to cancel the subscription at a specific moment in time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancelAt</span><span style="color: #BFC7D5;">(</span></div><div class="line"><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;">addDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">);</span></div>

Finally, you should always cancel user subscriptions before deleting the associated user
model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancelNow</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">delete</span><span style="color: #BFC7D5;">();</span></div>

Resuming Subscriptions

If a customer has canceled their subscription and you wish to resume it, you may invoke the
resume method on the subscription. The customer must still be within their «grace
period» in order to resume a subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">resume</span><span style="color: #BFC7D5;">();</span></div>

If the customer cancels a subscription and then resumes that subscription before the subscription
has fully expired the customer will not be billed immediately. Instead, their subscription will
be re-activated and they will be billed on the original billing cycle.

Subscription Trials

With Payment Method Up
Front

If you would like to offer trial periods to your customers while still collecting payment method
information up front, you should use the trialDays method when creating your
subscriptions:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$request</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</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>

This method will set the trial period ending date on the subscription record within the
database and instruct Stripe to not begin billing the customer until after this
date. When using the trialDays method, Cashier will overwrite any default trial
period configured for the price in Stripe.

exclamation

If the customer’s subscription is not canceled before the trial ending date they will be
charged as soon as the trial expires, so you should be sure to notify your users of their
trial ending date.

The trialUntil method allows you to provide a DateTime instance that
specifies when the trial period should end:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Carbon\</span><span style="color: #FFCB8B;">Carbon</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialUntil</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

You may determine if a user is within their trial period using either the onTrial
method of the user instance or the onTrial method of the subscription instance. The
two examples below are equivalent:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</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>

You may use the endTrial method to immediately end a subscription trial:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">endTrial</span><span style="color: #BFC7D5;">();</span></div>

To determine if an existing trial has expired, you may use the hasExpiredTrial
methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasExpiredTrial</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasExpiredTrial</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>

Defining Trial Days in Stripe / Cashier

You may choose to define how many trial days your price’s receive in the Stripe dashboard or
always pass them explicitly using Cashier. If you choose to define your price’s trial days in
Stripe you should be aware that new subscriptions, including new subscriptions for a customer
that had a subscription in the past, will always receive a trial period unless you explicitly
call the skipTrial() method.

Without Payment
Method Up Front

If you would like to offer trial periods without collecting the user’s payment method information
up front, you may set the trial_ends_at column on the user record to your desired
trial ending date. This is typically done during user registration:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">create</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">trial_ends_at</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;">now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">),</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>
exclamation

Be sure to add a date cast for the
trial_ends_at attribute within your billable model‘s class
definition.

Cashier refers to this type of trial as a «generic trial», since it is not attached to any
existing subscription. The onTrial method on the billable model
instance will return true if the current date is not past the value of
trial_ends_at:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> User is within their trial period...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Once you are ready to create an actual subscription for the user, you may use the
newSubscription method as usual:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

To retrieve the user’s trial ending date, you may use the trialEndsAt method. This
method will return a Carbon date instance if a user is on a trial or null if they
aren’t. You may also pass an optional subscription type parameter if you would like to get the
trial ending date for a specific subscription other than the default one:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$trialEndsAt</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialEndsAt</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">main</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

You may also use the onGenericTrial method if you wish to know specifically that the
user is within their «generic» trial period and has not yet created an actual subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGenericTrial</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> User is within their "generic" trial period...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Extending Trials

The extendTrial method allows you to extend the trial period of a subscription after
the subscription has been created. If the trial has already expired and the customer is already
being billed for the subscription, you can still offer them an extended trial. The time spent
within the trial period will be deducted from the customer’s next invoice:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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;"> End the trial 7 days from now...</span></div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">extendTrial</span><span style="color: #BFC7D5;">(</span></div><div class="line"><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;">addDays</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Add an additional 5 days to the trial...</span></div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">extendTrial</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">->trial_ends_at-></span><span style="color: #82AAFF;">addDays</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></div>

Handling Stripe Webhooks

lightbulb

You may use the Stripe CLI to help test
webhooks during local development.

Stripe can notify your application of a variety of events via webhooks. By default, a
route that points to Cashier’s webhook controller is automatically
registered by the Cashier service provider. This controller will handle all
incoming webhook requests.

By default, the Cashier webhook controller will automatically handle cancelling
subscriptions that have too many failed charges (as defined by your Stripe settings), customer
updates, customer deletions, subscription updates, and payment method changes; however, as we’ll
soon discover, you can extend this controller to handle any Stripe webhook event
you like.

To ensure your application can handle Stripe webhooks, be sure to configure the
webhook URL in the Stripe control panel. By default, Cashier’s webhook controller
responds to the /stripe/webhook URL path. The full list of all webhooks you should
enable in the Stripe control panel are:

For convenience, Cashier includes a cashier:webhook Artisan command. This command
will create a webhook in Stripe that listens to all of the events required by Cashier:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan cashier:webhook

By default, the created webhook will point to the URL defined by the APP_URL
environment variable and the cashier.webhook route that
is included with Cashier. You may provide the --url option when invoking the
command if you would like to use a different URL:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan cashier:webhook --url "https://example.com/stripe/webhook"

The webhook that is created will use the Stripe API version that your version of Cashier is
compatible with. If you would like to use a different Stripe version, you may provide the
--api-version option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan cashier:webhook --api-version="2019-12-03"

After creation, the webhook will be immediately active. If you wish to create the webhook but
have it disabled until you’re ready, you may provide the --disabled option when
invoking the command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan cashier:webhook --disabled

exclamation

Make sure you protect incoming Stripe webhook requests with Cashier’s included webhook signature verification
middleware.

Webhooks and CSRF
Protection

Since Stripe webhooks need to bypass Laravel’s CSRF protection, you
should ensure that Laravel does not attempt to validate the CSRF token for incoming Stripe
webhooks. To accomplish this, you should exclude stripe/* from CSRF protection in
your application’s bootstrap/app.php file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withMiddleware</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;">Middleware</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$<code>middleware) {
$middleware->validateCsrfTokens(except: [
'stripe/*',
]);
})

Defining Webhook
Event Handlers

Cashier automatically handles subscription cancellations for failed charges and other common
Stripe webhook events. However, if you have additional webhook events you would like to handle,
you may do so by listening to the following events that are dispatched by Cashier:

Both events contain the full payload of the Stripe webhook. For example, if you wish to handle
the invoice.payment_succeeded webhook, you may register a listener that will handle the event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Listeners;
 
use Laravel\Cashier\Events\WebhookReceived;
 
class StripeEventListener
{
/**
* Handle received Stripe webhooks.
*/
public function handle(WebhookReceived $event): void
{
if ($event->payload['type'] === 'invoice.payment_succeeded') {
// Handle the incoming event...
}
}
}

Verifying Webhook
Signatures

To secure your webhooks, you may use Stripe’s webhook signatures. For
convenience, Cashier automatically includes a middleware which validates that the
incoming Stripe webhook request is valid.

To enable webhook verification, ensure that the STRIPE_WEBHOOK_SECRET
environment variable is set in your application’s .env file. The
webhook secret may be retrieved from your Stripe account dashboard.

Single Charges

Simple Charge

If you would like to make a one-time charge against a customer, you may use the
charge method on a billable model instance. You will need to provide a payment method identifier as the
second argument to the charge method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/purchase</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: #BEC5D4;">$stripeCharge</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">charge</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">->paymentMethodId</span></div><div class="line"><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>

The charge method accepts an array as its third argument, allowing you to pass any
options you wish to the underlying Stripe charge creation. More information regarding the
options available to you when creating charges may be found in the Stripe documentation:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">, [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">custom_option</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;">$value</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

You may also use the charge method without an underlying customer or user. To
accomplish this, invoke the charge method on a new instance of your application’s
billable model:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$stripeCharge</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;">User</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div>

The charge method will throw an exception if the charge fails. If the charge is
successful, an instance of Laravel\Cashier\Payment will be returned from the
method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">try</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$payment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">catch</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB6B;">Exception</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$e</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>
exclamation

The charge method accepts the payment amount in the lowest denominator of the
currency used by your application. For example, if customers are paying in United States
Dollars, amounts should be specified in pennies.

Charge With Invoice

Sometimes you may need to make a one-time charge and offer a PDF invoice to your customer. The
invoicePrice method lets you do just that. For example, let’s invoice a customer
for five new shirts:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoicePrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div>

The invoice will be immediately charged against the user’s default payment method. The
invoicePrice method also accepts an array as its third argument. This array
contains the billing options for the invoice item. The fourth argument accepted by the method is
also an array which should contain the billing options for the invoice itself:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoicePrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</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: #D9F5DD;">'</span><span style="color: #C3E88D;">discounts</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;">coupon</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;">SUMMER21SALE</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;">default_tax_rates</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;">txr_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Similarly to invoicePrice, you may use the tabPrice method to create a
one-time charge for multiple items (up to 250 items per invoice) by adding them to the
customer’s «tab» and then invoicing the customer. For example, we may invoice a customer for
five shirts and two mugs:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tabPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tabPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_mug</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoice</span><span style="color: #BFC7D5;">();</span></div>

Alternatively, you may use the invoiceFor method to make a «one-off» charge against
the customer’s default payment method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoiceFor</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">One Time Fee</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">);</span></div>

Although the invoiceFor method is available for you to use, it is recommended that
you use the invoicePrice and tabPrice methods with pre-defined prices.
By doing so, you will have access to better analytics and data within your Stripe dashboard
regarding your sales on a per-product basis.

exclamation

The invoice, invoicePrice, and invoiceFor methods
will create a Stripe invoice which will retry failed billing attempts. If you do not want
invoices to retry failed charges, you will need to close them using the Stripe API after the
first failed charge.

Creating Payment Intents

You can create a new Stripe payment intent by invoking the pay method on a billable
model instance. Calling this method will create a payment intent that is wrapped in
a Laravel\Cashier\Payment instance:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/pay</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: #BEC5D4;">$payment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">pay</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">amount</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$payment</span><span style="color: #89DDFF;">->client_secret</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

After creating the payment intent, you can return the client secret to your application’s
frontend so that the user can complete the payment in their browser. To read more about building
entire payment flows using Stripe payment intents, please consult the Stripe
documentation
.

When using the pay method, the default payment methods that are enabled within your
Stripe dashboard will be available to the customer. Alternatively, if you only want to allow for
some specific payment methods to be used, you may use the payWith method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/pay</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: #BEC5D4;">$payment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">payWith</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">amount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">), [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">card</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">bancontact</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$payment</span><span style="color: #89DDFF;">->client_secret</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
exclamation

The pay and payWith methods accept the payment amount in the
lowest denominator of the currency used by your application. For example, if customers are
paying in United States Dollars, amounts should be specified in pennies.

Refunding Charges

If you need to refund a Stripe charge, you may use the refund method. This method
accepts the Stripe payment intent ID as its
first argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$payment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">100</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$paymentMethodId</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">refund</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$payment</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">);</span></div>

Invoices

Retrieving Invoices

You may easily retrieve an array of a billable model‘s invoices using the
invoices method. The invoices method returns a collection of
Laravel\Cashier\Invoice instances:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoices</span><span style="color: #BFC7D5;">();</span></div>

If you would like to include pending invoices in the results, you may use the
invoicesIncludingPending method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">invoicesIncludingPending</span><span style="color: #BFC7D5;">();</span></div>

You may use the findInvoice method to retrieve a specific invoice by its ID:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">findInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$invoiceId</span><span style="color: #BFC7D5;">);</span></div>

Displaying Invoice
Information

When listing the invoices for the customer, you may use the invoice’s methods to display the
relevant invoice information. For example, you may wish to list every invoice in a table,
allowing the user to easily download any of them:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">table</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">@</span><span style="color: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$invoices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">tr</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;">{{ </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">date</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toFormattedDateString</span><span style="color: #BFC7D5;">() }}</span><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;">{{ </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">total</span><span style="color: #BFC7D5;">() }}</span><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">><</span><span style="color: #82AAFF;">a</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">href</span><span style="color: #C792EA;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">/user/invoice/{{ </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">->id</span><span style="color: #C3E88D;"> }}</span><span style="color: #D9F5DD;">"</span><span style="color: #C792EA;">></span><span style="color: #82AAFF;">Download</span><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">a</span><span style="color: #C792EA;">><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">td</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">tr</span><span style="color: #C792EA;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">@</span><span style="color: #C792EA;">endforeach</span></div><div class="line"><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">/</span><span style="color: #82AAFF;">table</span><span style="color: #C792EA;">></span></div>

Upcoming Invoices

To retrieve the upcoming invoice for a customer, you may use the upcomingInvoice
method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">upcomingInvoice</span><span style="color: #BFC7D5;">();</span></div>

Similarly, if the customer has multiple subscriptions, you can also retrieve the upcoming invoice
for a specific subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">upcomingInvoice</span><span style="color: #BFC7D5;">();</span></div>

Previewing Subscription Invoices

Using the previewInvoice method, you can preview an invoice before
making price changes. This will allow you to determine what your customer’s invoice will look
like when a given price change is made:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pre<code>viewInvoice('price_yearly');

You may pass an array of prices to the previewInvoice method in order to
preview invoices with multiple new prices:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pre<code>viewInvoice(['price_yearly', 'price_metered']);

Generating Invoice PDFs

Before generating invoice PDFs, you should use Composer to install the Dompdf library, which is
the default invoice renderer for Cashier:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;"><code>composer require dompdf/dompdf

From within a route or controller, you may use the
downloadInvoice method to generate a PDF download of a given invoice. This method
will automatically generate the proper HTTP response needed to download the invoice:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/user/invoice/{invoice}</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: #BFC7D5;">, </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoiceId</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;">$request</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;">downloadInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$invoiceId</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

By default, all data on the invoice is derived from the customer and invoice data stored in
Stripe. The filename is based on your app.name config value. However,
you can customize some of this data by providing an array as the second argument to the
downloadInvoice method. This array allows you to customize information such as your
company and product details:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">downloadInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$invoiceId</span><span style="color: #BFC7D5;">, [</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">vendor</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;">Your Company</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</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;">Your Product</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;">street</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;">Main Str. 1</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;">location</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;">2000 Antwerp, Belgium</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;">phone</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;">+32 499 00 00 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;">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 class="__cf_email__" data-cfemail="4b22252d240b2e332a263b272e65282426" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;">url</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;">https://example.com</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;">vendorVat</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;">BE123456789</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

The downloadInvoice method also allows for a custom filename via its third argument.
This filename will automatically be suffixed with .pdf:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">downloadInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$invoiceId</span><span style="color: #BFC7D5;">, [], </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">my-invoice</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Custom Invoice Renderer

Cashier also makes it possible to use a custom invoice renderer. By default, Cashier uses the
DompdfInvoiceRenderer implementation, which utilizes the dompdf PHP library to generate Cashier’s
invoices. However, you may use any renderer you wish by implementing the
Laravel\Cashier\Contracts\InvoiceRenderer interface. For example, you may wish to
render an invoice PDF using an API call to a third-party PDF rendering service:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Http</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\Contracts\</span><span style="color: #FFCB8B;">InvoiceRenderer</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Invoice</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;">ApiInvoiceRenderer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">implements</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">InvoiceRenderer</span></div><div class="line"><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: #697098;">     * Render the given invoice and return the raw PDF bytes.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</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;">render</span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$data</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> [], </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$options</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> []</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</span></div><div class="line"><span style="color: #BFC7D5;">    {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$html</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>view($data)->render();
 
return Http::get('https://example.com/html-to-pdf', ['html' => $html])->get()->body();
}
}

Once you have implemented the invoice renderer contract, you should update the
cashier.invoices.renderer configuration value in your application’s
config/cashier.php configuration file. This configuration
value should be set to the class name of your custom renderer implementation.

Checkout

Cashier Stripe also provides support for Stripe
Checkout
. Stripe Checkout takes the pain out of implementing custom pages to accept
payments by providing a pre-built, hosted payment page.

The following documentation contains information on how to get started using Stripe Checkout with
Cashier. To learn more about Stripe Checkout, you should also consider reviewing Stripe’s own documentation on Checkout.

Product Checkouts

You may perform a checkout for an existing product that has been created within your Stripe
dashboard using the checkout method on a billable model. The
checkout method will initiate a new Stripe Checkout session. By default, you’re
required to pass a Stripe Price ID:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

If needed, you may also specify a product quantity:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</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;">15</span><span style="color: #BFC7D5;">]);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

When a customer visits this route they will be redirected to Stripe’s Checkout page.
By default, when a user successfully completes or cancels a purchase they will be redirected to
your home route location, but you may specify custom callback URLs
using the success_url and cancel_url options:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('your-success-route'),
'cancel_url' => route('your-cancel-route'),
]);
});

When defining your success_url checkout option, you may instruct Stripe to add the
checkout session ID as a query string parameter when invoking your URL. To do so, add the
literal string {CHECKOUT_SESSION_ID} to your success_url query string.
Stripe will replace this placeholder with the actual checkout session ID:

<!-- Syntax highlighted by torchlight.dev --><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;"> Stripe\Checkout\</span><span style="color: #FFCB8B;">Session</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Stripe\</span><span style="color: #FFCB8B;">Customer</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('checkout-success').'?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('checkout-cancel'),
]);
});
 
Route::get('/checkout-success', function (Request $request) {
$checkoutSession = $request->user()->stripe()->checkout->sessions->retrieve($request->get('session_id'));
 
return view('checkout.success', ['checkoutSession' => $checkoutSession]);
})->name('checkout-success');

Promotion Codes

By default, Stripe Checkout does not allow user redeemable
promotion codes
. Luckily, there’s an easy way to enable these for your Checkout page. To
do so, you may invoke the allowPromotionCodes method:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">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;">allowPromotionCodes</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Single Charge Checkouts

You can also perform a simple charge for an ad-hoc product that has not been created in your
Stripe dashboard. To do so you may use the checkoutCharge method on a billable
model and pass it a chargeable amount, a product name, and an optional quantity.
When a customer visits this route they will be redirected to Stripe’s Checkout
page:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/charge-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkoutCharge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1200</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">T-Shirt</span><span style="color: #D9F5DD;">'</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></div>
exclamation

When using the checkoutCharge method, Stripe will always create a new product
and price in your Stripe dashboard. Therefore, we recommend that you create the products up
front in your Stripe dashboard and use the checkout method instead.

Subscription Checkouts

exclamation

Using Stripe Checkout for subscriptions requires you to enable the
customer.subscription.created webhook in your Stripe dashboard. This webhook
will create the subscription record in your database and store all of the
relevant subscription items.

You may also use Stripe Checkout to initiate subscriptions. After defining your subscription with
Cashier’s subscription builder methods, you may call the checkout method. When a
customer visits this route they will be redirected to Stripe’s Checkout page:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Just as with product checkouts, you may customize the success and cancellation URLs:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">success_url</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;"><code>route('your-success-route'),
'cancel_url' => route('your-cancel-route'),
]);
});

Of course, you can also enable promotion codes for subscription checkouts:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">allowPromotionCodes</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
exclamation

Unfortunately Stripe Checkout does not support all subscription billing options when
starting subscriptions. Using the anchorBillingCycleOn method on the
subscription builder, setting proration behavior, or setting payment behavior will not have
any effect during Stripe Checkout sessions. Please consult the Stripe Checkout Session
API documentation
to review which parameters are available.

Stripe Checkout and
Trial Periods

Of course, you can define a trial period when building a subscription that will be completed
using Stripe Checkout:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialDays</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: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">();</span></div>

However, the trial period must be at least 48 hours, which is the minimum amount of trial time
supported by Stripe Checkout.

Subscriptions and Webhooks

Remember, Stripe and Cashier update subscription statuses via webhooks, so there’s a possibility
a subscription might not yet be active when the customer returns to the application after
entering their payment information. To handle this scenario, you may wish to display a message
informing the user that their payment or subscription is pending.

Collecting Tax IDs

Checkout also supports collecting a customer’s Tax ID. To enable this on a checkout session,
invoke the collectTaxIds method when creating the session:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collectTaxIds</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

When this method is invoked, a new checkbox will be available to the customer that allows them to
indicate if they’re purchasing as a company. If so, they will have the opportunity to provide
their Tax ID number.

exclamation

If you have already configured automatic tax
collection
in your application’s service provider then this feature will be enabled
automatically and there is no need to invoke the collectTaxIds method.

Guest Checkouts

Using the Checkout::guest method, you may initiate checkout sessions for guests of
your application that do not have an «account»:

<!-- Syntax highlighted by torchlight.dev --><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;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Checkout</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Checkout</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guest</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</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;">success_url</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;"><code>route('your-success-route'),
'cancel_url' => route('your-cancel-route'),
]);
});

Similarly to when creating checkout sessions for existing users, you may utilize additional
methods available on the Laravel\Cashier\CheckoutBuilder instance to customize the
guest checkout session:

<!-- Syntax highlighted by torchlight.dev --><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;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Checkout</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/product-checkout</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Checkout</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guest</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withPromotionCode</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">promo-code</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_tshirt</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;">success_url</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;"><code>route('your-success-route'),
'cancel_url' => route('your-cancel-route'),
]);
});

After a guest checkout has been completed, Stripe can dispatch a
checkout.session.completed webhook event, so make sure to configure your Stripe webhook
to actually send this event to your application. Once the webhook has been enabled within the
Stripe dashboard, you may handle the webhook with
Cashier
. The object contained in the webhook payload will be a checkout object
that you may inspect in order to fulfill your customer’s order.

Handling Failed Payments

Sometimes, payments for subscriptions or single charges can fail. When this happens, Cashier will
throw an Laravel\Cashier\Exceptions\IncompletePayment exception that informs you
that this happened. After catching this exception, you have two options on how to proceed.

First, you could redirect your customer to the dedicated payment confirmation page which is
included with Cashier. This page already has an associated named route that is
registered via Cashier’s service provider. So, you may catch the IncompletePayment
exception and redirect the user to the payment confirmation page:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\Exceptions\</span><span style="color: #FFCB8B;">IncompletePayment</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">try</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">newSubscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$paymentMethod</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">catch</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB6B;">IncompletePayment</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$exception</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>route(
'cashier.payment',
[$exception->payment->id, 'redirect' => route('home')]
);
}

On the payment confirmation page, the customer will be prompted to enter their credit card
information again and perform any additional actions required by Stripe, such as «3D Secure»
confirmation. After confirming their payment, the user will be redirected to the URL provided by
the redirect parameter specified above. Upon redirection, message
(string) and success (integer) query string variables will be added to the URL. The
payment page currently supports the following payment method types:

  • Credit Cards
  • Alipay
  • Bancontact
  • BECS Direct Debit
  • EPS
  • Giropay
  • iDEAL
  • SEPA Direct Debit

Alternatively, you could allow Stripe to handle the payment confirmation for you. In this case,
instead of redirecting to the payment confirmation page, you may setup Stripe’s automatic
billing emails
in your Stripe dashboard. However, if an IncompletePayment
exception is caught, you should still inform the user they will receive an email with further
payment confirmation instructions.

Payment exceptions may be thrown for the following methods: charge,
invoiceFor, and invoice on models using the
Billable trait. When interacting with subscriptions, the create method
on the SubscriptionBuilder, and the incrementAndInvoice and
swapAndInvoice methods on the Subscription and
SubscriptionItem models may throw incomplete payment exceptions.

Determining if an existing subscription has an incomplete payment may be accomplished using the
hasIncompletePayment method on the billable model or a subscription
instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasIncompletePayment</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasIncompletePayment</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>

You can derive the specific status of an incomplete payment by inspecting the
payment property on the exception instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\Exceptions\</span><span style="color: #FFCB8B;">IncompletePayment</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">try</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">1000</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pm_card_threeDSecure2Required</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">catch</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB6B;">IncompletePayment</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$exception</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;"> Get the payment intent status...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$exception</span><span style="color: #89DDFF;">->payment->status</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;"> Check specific conditions...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$exception</span><span style="color: #89DDFF;">->payment-></span><span style="color: #82AAFF;">requiresPaymentMethod</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><span style="color: #C792EA;">elseif</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$exception</span><span style="color: #89DDFF;">->payment-></span><span style="color: #82AAFF;">requiresConfirmation</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><div class="line"><span style="color: #BFC7D5;">}</span></div>

Confirming Payments

Some payment methods require additional data in order to confirm payments. For example, SEPA
payment methods require additional «mandate» data during the payment process. You may provide
this data to Cashier using the withPaymentConfirmationOptions method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withPaymentConfirmationOptions</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mandate_data</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;">...</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_xxx</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may consult the Stripe API
documentation
to review all of the options accepted when confirming
payments.

Strong Customer
Authentication

If your business or one of your customers is based in Europe you will need to abide by the EU’s
Strong Customer Authentication (SCA) regulations. These regulations were imposed in September
2019 by the European Union to prevent payment fraud. Luckily, Stripe and Cashier are prepared
for building SCA compliant applications.

exclamation

Before getting started, review Stripe’s guide on PSD2
and SCA
as well as their documentation on the new
SCA APIs
.

Payments Requiring Additional
Confirmation

SCA regulations often require extra verification in order to confirm and process a payment. When
this happens, Cashier will throw a Laravel\Cashier\Exceptions\IncompletePayment
exception that informs you that extra verification is needed. More information on how to handle
these exceptions be found can be found in the documentation on handling failed payments.

Payment confirmation screens presented by Stripe or Cashier may be tailored to a specific bank or
card issuer’s payment flow and can include additional card confirmation, a temporary small
charge, separate device authentication, or other forms of verification.

Incomplete and Past
Due State

When a payment needs additional confirmation, the subscription will remain in an
incomplete or past_due state as indicated by its
stripe_status database column. Cashier will automatically activate the
customer’s subscription as soon as payment confirmation is complete and your application is
notified by Stripe via webhook of its completion.

For more information on incomplete and past_due states, please refer to
our additional documentation on these states.

Off-Session
Payment Notifications

Since SCA regulations require customers to occasionally verify their payment details even while
their subscription is active, Cashier can send a notification to the customer when off-session
payment confirmation is required. For example, this may occur when a subscription is renewing.
Cashier’s payment notification can be enabled by setting the
CASHIER_PAYMENT_NOTIFICATION environment variable to a notification
class. By default, this notification is disabled. Of course, Cashier includes a notification
class you may use for this purpose, but you are free to provide your own notification class if
desired:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_PAYMENT_NOTIFICATION</span><span style="color: #BFC7D5;">=Laravel\Cashier\Notifications\ConfirmPayment</span></div>

To ensure that off-session payment confirmation notifications are delivered, verify that Stripe webhooks are configured for your
application and the invoice.payment_action_required webhook is enabled in your
Stripe dashboard. In addition, your Billable model should also use
Laravel’s Illuminate\Notifications\Notifiable trait.

exclamation

Notifications will be sent even when customers are manually making a payment that requires
additional confirmation. Unfortunately, there is no way for Stripe to know that the payment
was done manually or «off-session». But, a customer will simply see a «Payment Successful»
message if they visit the payment page after already confirming their payment. The customer
will not be allowed to accidentally confirm the same payment twice and incur an accidental
second charge.

Stripe SDK

Many of Cashier’s objects are wrappers around Stripe SDK objects. If you would like to interact
with the Stripe objects directly, you may conveniently retrieve them using the
asStripe method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$stripeSubscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">asStripeSubscription</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$stripeSubscription</span><span style="color: #89DDFF;">->application_fee_percent</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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;">$stripeSubscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">save</span><span style="color: #BFC7D5;">();</span></div>

You may also use the updateStripeSubscription method to update a Stripe subscription
directly:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateStripeSubscription</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">application_fee_percent</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;">5</span><span style="color: #BFC7D5;">]);</span></div>

You may invoke the stripe method on the Cashier class if you would like
to use the Stripe\StripeClient client directly. For example, you could use this
method to access the StripeClient instance and retrieve a list of prices from your
Stripe account:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Cashier\</span><span style="color: #FFCB8B;">Cashier</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">stripe</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">->prices-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div>

Testing

When testing an application that uses Cashier, you may mock the actual HTTP requests to the
Stripe API; however, this requires you to partially re-implement Cashier’s own behavior.
Therefore, we recommend allowing your tests to hit the actual Stripe API. While this is slower,
it provides more confidence that your application is working as expected and any slow tests may
be placed within their own Pest / PHPUnit testing group.

When testing, remember that Cashier itself already has a great test suite, so you should only
focus on testing the subscription and payment flow of your own application and not every
underlying Cashier behavior.

To get started, add the testing version of your Stripe secret to your
phpunit.xml file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;"><</span><span style="color: #82AAFF;"><code>env name="STRIPE_SECRET" value="sk_test_"/>

Now, whenever you interact with Cashier while testing, it will send actual API requests to your
Stripe testing environment. For convenience, you should pre-fill your Stripe
testing account with subscriptions / prices that you may use during testing.

lightbulb

In order to test a variety of billing scenarios, such as credit card denials and failures,
you may use the vast range of testing card numbers
and tokens
provided by Stripe.



Blade

Blade Templates

Introduction

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some
PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.
In fact, all Blade templates are compiled into plain PHP code and cached until they are
modified, meaning Blade adds essentially zero overhead to your application. Blade template files
use the .blade.php file extension and are typically stored in the
resources/views directory.

Blade views may be returned from routes or controllers
using the global view helper. Of course, as mentioned in the documentation on views, data may be passed to the Blade view
using the view helper’s second argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/</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: #82AAFF;"><code>view('greeting', ['name' => 'Finn']);
});

Supercharging Blade With Livewire

Want to take your Blade templates to the next level and build dynamic interfaces with ease? Check
out Laravel Livewire. Livewire allows you to write
Blade components that are augmented with dynamic functionality that would typically only be
possible via frontend frameworks like React or Vue, providing a great approach to building
modern, reactive frontends without the complexities, client-side rendering, or
build steps of many JavaScript frameworks.

Displaying Data

You may display data that is passed to your Blade views by wrapping the variable in
curly braces. For example, given the following route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Route</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;">/</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: #82AAFF;"><code>view('welcome', ['name' => 'Samantha']);
});

You may display the contents of the name variable like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Hello, </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;">.</span></div>

lightbulb

Blade’s {{ }} echo statements are automatically sent through PHP’s
htmlspecialchars function to prevent XSS attacks.

You are not limited to displaying the contents of the variables passed to the view.
You may also echo the results of any PHP function. In fact, you can put any PHP code you wish
inside of a Blade echo statement:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">The current UNIX timestamp is </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">time</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;">.</span></div>

HTML Entity Encoding

By default, Blade (and the Laravel e function) will double encode HTML entities. If
you would like to disable double encoding, call the Blade::withoutDoubleEncoding
method from the boot method of your AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Providers;
 
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::withoutDoubleEncoding();
}
}

Displaying Unescaped
Data

By default, Blade {{ }} statements are automatically sent through PHP’s
htmlspecialchars function to prevent XSS attacks. If you do not want your data to
be escaped, you may use the following syntax:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Hello, </span><span style="color: #89DDFF;">{!!</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">!!}</span><span style="color: #BFC7D5;">.</span></div>

exclamation

Be very careful when echoing content that is supplied by users of your application. You
should typically use the escaped, double curly brace syntax to prevent XSS attacks when
displaying user supplied data.

Blade and
JavaScript Frameworks

Since many JavaScript frameworks also use «curly» braces to indicate a given expression should be
displayed in the browser, you may use the @ symbol to inform the Blade rendering
engine an expression should remain untouched. For example:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">h1</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Laravel</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">h1</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">Hello, </span><span style="color: #FFCB6B;">@</span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;">.</span></div>

In this example, the @ symbol will be removed by Blade; however,
{{ name }} expression will remain untouched by the Blade engine, allowing it to be
rendered by your JavaScript framework.

The @ symbol may also be used to escape Blade directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">{{--</span><span style="color: #697098;"> Blade template </span><span style="color: #697098;">--}}</span></div><div class="line"><span style="color: #BFC7D5;">@@if()</span></div><div class="line"> </div><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> HTML output </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@if</span><span style="color: #BFC7D5;">()</span></div>

Rendering JSON

Sometimes you may pass an array to your view with the intention of rendering it as
JSON in order to initialize a JavaScript variable. For example:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">var</span><span style="color: #BFC7D5;"> app </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</span><span style="color: #89DDFF;">?</span><span style="color: #BFC7D5;"><code>php echo json_encode($array); ?>;
</script>

However, instead of manually calling json_encode, you may use the
Illuminate\Support\Js::from method directive. The from method accepts
the same arguments as PHP’s json_encode function; however, it will ensure that the
resulting JSON is properly escaped for inclusion within HTML quotes. The from
method will return a string JSON.parse JavaScript statement that will convert the
given object or array into a valid JavaScript object:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">var</span><span style="color: #BFC7D5;"> app </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> {{ Illuminate\Support\Js::</span><span style="color: #82AAFF;">from</span><span style="color: #BFC7D5;">($array) }};</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div>

The latest versions of the Laravel application skeleton include a Js facade, which
provides convenient access to this functionality within your Blade templates:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">var</span><span style="color: #BFC7D5;"> app </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> {{ Js::</span><span style="color: #82AAFF;">from</span><span style="color: #BFC7D5;">($array) }};</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div>

exclamation

You should only use the Js::from method to render existing variables as JSON.
The Blade templating is based on regular expressions and attempts to pass a complex
expression to the directive may cause unexpected failures.

The @verbatim
Directive

If you are displaying JavaScript variables in a large portion of your template, you may wrap the
HTML in the @verbatim directive so that you do not have to prefix each Blade echo
statement with an @ symbol:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@verbatim</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">container</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">        Hello, </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">name</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: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endverbatim</span></div>

Blade Directives

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts
for common PHP control structures, such as conditional statements and loops. These shortcuts
provide a very clean, terse way of working with PHP control structures while also remaining
familiar to their PHP counterparts.

If Statements

You may construct if statements using the @if, @elseif,
@else, and @endif directives. These directives function identically to
their PHP counterparts:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">records</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"><span style="color: #BFC7D5;">    I have one record!</span></div><div class="line"><span style="color: #C792EA;">@elseif </span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">records</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"><span style="color: #BFC7D5;">    I have multiple records!</span></div><div class="line"><span style="color: #C792EA;">@else</span></div><div class="line"><span style="color: #BFC7D5;">    I don't have any records!</span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

For convenience, Blade also provides an @unless directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@unless </span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB6B;">Auth</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">check</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;">    You are not signed in.</span></div><div class="line"><span style="color: #C792EA;">@endunless</span></div>

In addition to the conditional directives already discussed, the @isset and
@empty directives may be used as convenient shortcuts for their respective PHP
functions:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@isset</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$records</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">    // $records is defined and is not null...</span></div><div class="line"><span style="color: #C792EA;">@endisset</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@empty</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$records</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">    // $records is "empty"...</span></div><div class="line"><span style="color: #C792EA;">@endempty</span></div>

Authentication
Directives

The @auth and @guest directives may be used to quickly determine if the
current user is authenticated or is a guest:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@auth</span></div><div class="line"><span style="color: #BFC7D5;">    // The user is authenticated...</span></div><div class="line"><span style="color: #C792EA;">@endauth</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@guest</span></div><div class="line"><span style="color: #BFC7D5;">    // The user is not authenticated...</span></div><div class="line"><span style="color: #C792EA;">@endguest</span></div>

If needed, you may specify the authentication guard that should be checked when using the
@auth and @guest directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@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></div><div class="line"><span style="color: #BFC7D5;">    // The user is authenticated...</span></div><div class="line"><span style="color: #C792EA;">@endauth</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@guest</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></div><div class="line"><span style="color: #BFC7D5;">    // The user is not authenticated...</span></div><div class="line"><span style="color: #C792EA;">@endguest</span></div>

Environment Directives

You may check if the application is running in the production environment using the
@production directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@production</span></div><div class="line"><span style="color: #BFC7D5;">    // Production specific content...</span></div><div class="line"><span style="color: #C792EA;">@endproduction</span></div>

Or, you may determine if the application is running in a specific environment using
the @env directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@<code>env('staging')
// The application is running in "staging"...
@endenv
 
@env(['staging', 'production'])
// The application is running in "staging" or "production"...
@endenv

Section Directives

You may determine if a template inheritance section has content using the
@hasSection directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@hasSection</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">navigation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">pull-right</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@yield</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">navigation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</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;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">clearfix</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

You may use the sectionMissing directive to determine if a section does not have
content:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@sectionMissing</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">navigation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">pull-right</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@include</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default-navigation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

Session Directives

The @session directive may be used to determine if a session value exists. If the session value exists, the template
contents within the @session and @endsession directives will be
evaluated. Within the @session directive’s contents, you may echo the
$value variable to display the session value:

<!-- 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;">status</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">p-4 bg-green-100</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #82AAFF;">@endsession</span></div>

Switch Statements

Switch statements can be constructed using the @switch, @case,
@break, @default and @endswitch directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@switch</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$i</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@case</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;">        First case...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@break</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@case</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;">        Second case...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@break</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@default</span></div><div class="line"><span style="color: #BFC7D5;">        Default case...</span></div><div class="line"><span style="color: #C792EA;">@endswitch</span></div>

Loops

In addition to conditional statements, Blade provides simple directives for working with PHP’s
loop structures. Again, each of these directives functions identically to their PHP
counterparts:

<!-- 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;">    The current value is </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$i</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #C792EA;">@endfor</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">This is user </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@forelse </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@empty</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">No users</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endforelse</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@while </span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">I'm looping forever.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endwhile</span></div>

lightbulb

While iterating through a foreach loop, you may use the loop variable to gain valuable information about the loop,
such as whether you are in the first or last iteration through the loop.

When using loops you may also skip the current iteration or end the loop using the
@continue and @break directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->type</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"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@continue</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">==</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: #C792EA;">@break</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>

You may also include the continuation or break condition within the directive declaration:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #C792EA;">@continue</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->type</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: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@break</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->number</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">==</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>

The Loop Variable

While iterating through a foreach loop, a $loop variable will be
available inside of your loop. This variable provides access to some useful bits of information
such as the current loop index and whether this is the first or last iteration through the loop:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$loop</span><span style="color: #89DDFF;">->first</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">        This is the first iteration.</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$loop</span><span style="color: #89DDFF;">->last</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">        This is the last iteration.</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">This is user </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>

If you are in a nested loop, you may access the parent loop’s $loop variable via the
parent property:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->posts</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$post</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$loop</span><span style="color: #89DDFF;">->parent->first</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">            This is the first iteration of the parent loop.</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>

The $loop variable also contains a variety of other useful properties:

Property Description
$loop->index The index of the current loop iteration (starts at 0).
$loop->iteration The current loop iteration (starts at 1).
$loop->remaining The iterations remaining in the loop.
$loop->count The total number of items in the array being iterated.
$loop->first Whether this is the first iteration through the loop.
$loop->last Whether this is the last iteration through the loop.
$loop->even Whether this is an even iteration through the loop.
$loop->odd Whether this is an odd iteration through the loop.
$loop->depth The nesting level of the current loop.
$loop->parent When in a nested loop, the parent’s loop variable.

Conditional Classes & Styles

The @class directive conditionally compiles a CSS class string. The directive
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:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;">@<code>php
$isActive = false;
$hasError = true;
@endphp
 
<span @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
'bg-red' => $hasError,
])></spanspan>
 
<span class="p-4 text-gray-500 bg-red"></spanspan>

Likewise, the @style directive may be used to conditionally add inline CSS styles to
an HTML element:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;">@<code>php
$isActive = true;
@endphp
 
<span @style([
'background-color: red',
'font-weight: bold' => $isActive,
])></spanspan>
 
<span style="background-color: red; font-weight: bold;"></spanspan>

Additional Attributes

For convenience, you may use the @checked directive to easily indicate if a given
HTML checkbox input is «checked». This directive will echo checked if the provided
condition evaluates to true:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">input</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">checkbox</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">name</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">active</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">value</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">active</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #82AAFF;">@checked</span><span style="color: #89DDFF;">(</span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">active</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;">->active</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">)</span></div><div class="line"><span style="color: #89DDFF;">/></span></div>

Likewise, the @selected directive may be used to indicate if a given select option
should be «selected»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">select</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">name</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">version</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$product</span><span style="color: #89DDFF;">->versions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$version</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">option</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">value</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">{{</span><span style="color: #C3E88D;"> </span><span style="color: #BEC5D4;">$version</span><span style="color: #C3E88D;"> </span><span style="color: #89DDFF;">}}</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #82AAFF;">@selected</span><span style="color: #89DDFF;">(</span><span style="color: #82AAFF;">old</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">version</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;"> </span><span style="color: #C792EA;">==</span><span style="color: #89DDFF;"> </span><span style="color: #BEC5D4;">$version</span><span style="color: #89DDFF;">)></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$version</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">option</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">select</span><span style="color: #89DDFF;">></span></div>

Additionally, the @disabled directive may be used to indicate if a given element
should be «disabled»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">submit</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #82AAFF;">@disabled</span><span style="color: #89DDFF;">(</span><span style="color: #BEC5D4;">$errors</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotEmpty</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">)></span><span style="color: #BFC7D5;">Submit</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

Moreover, the @readonly directive may be used to indicate if a given element should
be «readonly»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">input</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">name</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">email</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">value</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="76131b171f1a361a17041700131a5815191b" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #82AAFF;">@readonly</span><span style="color: #89DDFF;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotAdmin</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">)</span></div><div class="line"><span style="color: #89DDFF;">/></span></div>

In addition, the @required directive may be used to indicate if a given element
should be «required»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">input</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">text</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">name</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">value</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #89DDFF;"> </span><span style="color: #82AAFF;">@required</span><span style="color: #89DDFF;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isAdmin</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">)</span></div><div class="line"><span style="color: #89DDFF;">/></span></div>

Including Subviews

lightbulb

While you’re free to use the @include directive, Blade components provide similar functionality and offer several
benefits over the @include directive such as data and attribute binding.

Blade’s @include directive allows you to include a Blade view from
within another view. All variables that are available to the parent
view will be made available to the included view:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@include</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">shared.errors</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Form Contents </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Even though the included view will inherit all data available in the parent
view, you may also pass an array of additional data that should be made available
to the included view:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@include</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view.name', ['status' => 'complete'])

If you attempt to @include a view which does not exist, Laravel will
throw an error. If you would like to include a view that may or may not be present,
you should use the @includeIf directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@includeIf</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view.name', ['status' => 'complete'])

If you would like to @include a view if a given boolean expression
evaluates to true or false, you may use the @includeWhen
and @includeUnless directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@includeWhen</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$boolean</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view.name', ['status' => 'complete'])
 
@includeUnless($boolean, 'view.name', ['status' => 'complete'])

To include the first view that exists from a given array of views, you
may use the includeFirst directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@includeFirst</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">custom.admin</span><span style="color: #D9F5DD;">'</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: #D9F5DD;">'</span><span style="color: #C3E88D;">status</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;">complete</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span></div>

exclamation

You should avoid using the __DIR__ and __FILE__ constants in your
Blade views, since they will refer to the location of the cached, compiled
view.

Rendering Views
for Collections

You may combine loops and includes into one line with Blade’s @each directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@each</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view.name', $jobs, 'job')

The @each directive’s first argument is the view to render for each
element in the array or collection. The second argument is the array or collection you wish to
iterate over, while the third argument is the variable name that will be assigned to the current
iteration within the view. So, for example, if you are iterating over an array of
jobs, typically you will want to access each job as a job variable
within the view. The array key for the current iteration will be available as the
key variable within the view.

You may also pass a fourth argument to the @each directive. This argument determines
the view that will be rendered if the given array is empty.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@each</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>view.name', $jobs, 'job', 'view.empty')

exclamation

Views rendered via @each do not inherit the variables from the parent
view. If the child view requires these variables, you should use
the @foreach and @include directives instead.

The @once Directive

The @once directive allows you to define a portion of the template that will only be
evaluated once per rendering cycle. This may be useful for pushing a given piece of JavaScript
into the page’s header using stacks. For example, if you are rendering a
given component within a loop, you may wish to only push the
JavaScript to the header the first time the component is rendered:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@once</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Your custom JavaScript...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endpush</span></div><div class="line"><span style="color: #C792EA;">@endonce</span></div>

Since the @once directive is often used in conjunction with the @push
or @prepend directives, the @pushOnce and @prependOnce
directives are available for your convenience:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@pushOnce</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Your custom JavaScript...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endPushOnce</span></div>

Raw PHP

In some situations, it’s useful to embed PHP code into your views. You can use the
Blade @php directive to execute a block of plain PHP within your template:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;">@<code>php
$counter = 1;
@endphp

Or, if you only need to use PHP to import a class, you may use the @use directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@use</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">App\Models\Flight</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div>

A second argument may be provided to the @use directive to alias the imported class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">@</span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;">('App\Models\</span><span style="color: #FFCB8B;">Flight</span><span style="color: #BFC7D5;">', '</span><span style="color: #FFCB8B;">FlightModel</span><span style="color: #BFC7D5;">')</span></div>

Comments

Blade also allows you to define comments in your views. However, unlike HTML
comments, Blade comments are not included in the HTML returned by your application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">{{--</span><span style="color: #697098;"> This comment will not be present in the rendered HTML </span><span style="color: #697098;">--}}</span></div>

Components

Components and slots provide similar benefits to sections, layouts, and includes; however, some
may find the mental model of components and slots easier to understand. There are
two approaches to writing components: class based components and anonymous components.

To create a class based component, you may use the make:component Artisan command.
To illustrate how to use components, we will create a simple Alert component. The
make:component command will place the component in the
app/View/Components directory:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:component Alert

The make:component command will also create a view template for the
component. The view will be placed in the resources/views/components
directory. When writing components for your own application, components are automatically
discovered within the app/View/Components directory and
resources/views/components directory, so no further component registration is
typically required.

You may also create components within subdirectories:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:component Forms/Input

The command above will create an Input component in the
app/View/Components/Forms directory and the view will be placed in the
resources/views/components/forms directory.

If you would like to create an anonymous component (a component with only a Blade template and no
class), you may use the --view flag when invoking the make:component
command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:component forms.input --view

The command above will create a Blade file at
resources/views/components/forms/input.blade.php which can be rendered as a
component via <x-forms.input />.

Manually Registering Package Components

When writing components for your own application, components are automatically discovered within
the app/View/Components directory and resources/views/components
directory.

However, if you are building a package that utilizes Blade components, you will need to manually
register your component class and its HTML tag alias. You should typically register your
components in the boot method of your package’s service provider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</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 your package's 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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">component</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">package-alert</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Alert</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>

Once your component has been registered, it may be rendered using its tag alias:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-package-alert</span><span style="color: #89DDFF;">/></span></div>

Alternatively, you may use the componentNamespace method to autoload component
classes by convention. For example, a Nightshade package might have
Calendar and ColorPicker components that reside within the
Package\Views\Components namespace:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</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 your package's 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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">componentNamespace</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Nightshade</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Views</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Components</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">nightshade</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

This will allow the usage of package components by their vendor namespace using the
package-name:: syntax:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-nightshade::calendar</span><span style="color: #89DDFF;"> /></span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-nightshade::color-picker</span><span style="color: #89DDFF;"> /></span></div>

Blade will automatically detect the class that’s linked to this component by pascal-casing the
component name. Subdirectories are also supported using «dot» notation.

Rendering Components

To display a component, you may use a Blade component tag within one of your Blade templates.
Blade component tags start with the string x- followed by the kebab case name of
the component class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">/></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-user-profile</span><span style="color: #89DDFF;">/></span></div>

If the component class is nested deeper within the app/View/Components directory,
you may use the . character to indicate directory nesting. For example, if we
assume a component is located at app/View/Components/Inputs/Button.php, we may
render it like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-inputs.button</span><span style="color: #89DDFF;">/></span></div>

If you would like to conditionally render your component, you may define a
shouldRender method on your component class. If the shouldRender
method returns false the component will not be rendered:

<!-- 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: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Whether the component should be rendered</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;">shouldRender</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</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: #FFCB8B;">Str</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">length</span><span style="color: #BFC7D5;">(</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->message</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></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Index Components

Sometimes components are part of a component group and you may wish to group the related
components within a single directory. For example, imagine a «card» component with the following
class structure:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">App\Views\Components\Card\Card</span></div><div class="line"><span style="color: #BFC7D5;">App\Views\Components\Card\Header</span></div><div class="line"><span style="color: #BFC7D5;">App\Views\Components\Card\Body</span></div>

Since the root Card component is nested within a Card directory, you
might expect that you would need to render the component via <x-card.card>.
However, when a component’s file name matches the name of the component’s directory, Laravel
automatically assumes that component is the «root» component and allows you to render the
component without repeating the directory name:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-card</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-card.header</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">...</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-card.header</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-card.body</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">...</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-card.body</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-card</span><span style="color: #89DDFF;">></span></div>

Passing Data to
Components

You may pass data to Blade components using HTML attributes. Hard-coded, primitive values may be
passed to the component using simple HTML attribute strings. PHP expressions and variables
should be passed to the component via attributes that use the : character as a
prefix:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">error</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:message</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$message</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">/></span></div>

You should define all of the component’s data attributes in its class constructor. All public
properties on a component will automatically be made available to the component’s
view. It is not necessary to pass the data to the view from the
component’s render method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\View\Components;
 
use Illuminate\View\Component;
use Illuminate\View\View;
 
class Alert extends Component
{
/**
* Create the component instance.
*/
public function __construct(
public string $type,
public string $message,
) {}
 
/**
* Get the view / contents that represent the component.
*/
public function render(): View
{
return view('components.alert');
}
}

When your component is rendered, you may display the contents of your component’s public
variables by echoing the variables by name:

<!-- 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: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">alert alert-</span><span style="color: #89DDFF;">{{</span><span style="color: #C3E88D;"> </span><span style="color: #BEC5D4;">$type</span><span style="color: #C3E88D;"> </span><span style="color: #89DDFF;">}}</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$message</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Casing

Component constructor arguments should be specified using camelCase, while
kebab-case should be used when referencing the argument names in your HTML
attributes. For example, given the following component constructor:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Create the component instance.</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: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</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;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$alertType</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {}</span></div>

The $alertType argument may be provided to the component like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">alert-type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">danger</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> /></span></div>

Short Attribute Syntax

When passing attributes to components, you may also use a «short attribute» syntax. This is often
convenient since attribute names frequently match the variable names they correspond to:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">{{--</span><span style="color: #697098;"> Short attribute syntax... </span><span style="color: #697098;">--}}</span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-profile</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:$userId</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:$name</span><span style="color: #89DDFF;"> /></span></div><div class="line"> </div><div class="line"><span style="color: #697098;">{{--</span><span style="color: #697098;"> Is equivalent to... </span><span style="color: #697098;">--}}</span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-profile</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:user-id</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$userId</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:name</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$name</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> /></span></div>

Escaping Attribute
Rendering

Since some JavaScript frameworks such as Alpine.js also use colon-prefixed attributes, you may
use a double colon (::) prefix to inform Blade that the attribute is not a PHP
expression. For example, given the following component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">::class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">{ danger: isDeleting }</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Submit</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-button</span><span style="color: #89DDFF;">></span></div>

The following HTML will be rendered by Blade:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">{ danger: isDeleting }</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Submit</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

Component Methods

In addition to public variables being available to your component template, any public methods on
the component may be invoked. For example, imagine a component that has an
isSelected method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Determine if the given option is the currently selected option.</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;">isSelected</span><span style="color: #D9F5DD;">(</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$option</span><span style="color: #D9F5DD;">)</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</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: #BEC5D4;">$option</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->selected</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

You may execute this method from your component template by invoking the variable matching the
name of the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">option</span><span style="color: #89DDFF;"> {{ </span><span style="color: #82AAFF;">$</span><span style="color: #BEC5D4;">isSelected</span><span style="color: #89DDFF;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #89DDFF;">) ? </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">selected</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;"> : </span><span style="color: #D9F5DD;">''</span><span style="color: #89DDFF;"> }} </span><span style="color: #FFCB6B;">value</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">{{</span><span style="color: #C3E88D;"> </span><span style="color: #BEC5D4;">$value</span><span style="color: #C3E88D;"> </span><span style="color: #89DDFF;">}}</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$label</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">option</span><span style="color: #89DDFF;">></span></div>

Accessing Attributes and Slots Within
Component Classes

Blade components also allow you to access the component name, attributes, and slot inside the
class’s render method. However, in order to access this data, you should return a closure from
your component’s render method:

<!-- 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"> </div><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the <code>view / contents that represent the component.
*/
public function render(): Closure
{
return function () {
return '
Components content
';
};
}

The closure returned by your component’s render method may also receive a
$data array as its only argument. This array will contain several elements that
provide information about the component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</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;">$data</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;"> $data['componentName'];</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> $data['attributes'];</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> $data['slot'];</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: #D9F5DD;">'</span><span style="color: #C3E88D;"><div $attributes="" {{="" }}="">Components content</div></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
exclamation

The elements in the $data array should never be directly embedded into the
Blade string returned by your render method, as doing so could allow remote
code execution via malicious attribute content.

The componentName is equal to the name used in the HTML tag after the
x- prefix. So <x-alert />‘s componentName will be
alert. The attributes element will contain all of the attributes that
were present on the HTML tag. The slot element is an
Illuminate\Support\HtmlString instance with the contents of the component’s slot.

The closure should return a string. If the returned string corresponds to an existing
view, that view will be rendered; otherwise, the returned string will
be evaluated as an inline Blade view.

Additional Dependencies

If your component requires dependencies from Laravel’s service
container
, you may list them before any of the component’s data attributes and they will
automatically be injected by the container:

<!-- 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;">AlertCreator</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;"> * Create the component instance.</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: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">AlertCreator</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$creator</span><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;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$type</span><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;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$message</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {}</span></div>

Hiding Attributes /
Methods

If you would like to prevent some public methods or properties from being exposed as variables to
your component template, you may add them to an $except array property on your
component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\View\Components;
 
use Illuminate\View\Component;
 
class Alert extends Component
{
/**
* The properties / methods that should not be exposed to the component template.
*
* @var array
*/
protected $except = ['type'];
 
/**
* Create the component instance.
*/
public function __construct(
public string $type,
) {}
}

Component Attributes

We’ve already examined how to pass data attributes to a component; however, sometimes you may
need to specify additional HTML attributes, such as class, that are not part of the
data required for a component to function. Typically, you want to pass these additional
attributes down to the root element of the component template. For example, imagine we want to
render an alert component like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">error</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:message</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$message</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mt-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">/></span></div>

All of the attributes that are not part of the component’s constructor will automatically be
added to the component’s «attribute bag». This attribute bag is automatically made available to
the component via the $attributes variable. All of the attributes may be rendered
within the component by echoing this variable:

<!-- 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: #BEC5D4;">$attributes</span><span style="color: #89DDFF;"> }}></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Component content </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

exclamation

Using directives such as @env within component tags is not supported at this
time. For example, <x-alert :live="@env('production')"/> will not be
compiled.

Default / Merged
Attributes

Sometimes you may need to specify default values for attributes or merge additional values into
some of the component’s attributes. To accomplish this, you may use the attribute bag’s
merge method. This method is particularly useful for defining a set of default CSS
classes that should always be applied to a component:

<!-- 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: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">class</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;"> => </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">alert alert-</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$type</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;"> }}></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$message</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

If we assume this component is utilized like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">error</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:message</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$message</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mb-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">/></span></div>

The final, rendered HTML of the component will appear like the following:

<!-- 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: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">alert alert-error mb-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Contents of the $message variable </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Conditionally Merge
Classes

Sometimes you may wish to merge classes if a given condition is true. You can
accomplish this via the class method, which 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:

<!-- 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: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">class</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: #89DDFF;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">bg-red</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;"> => </span><span style="color: #BEC5D4;">$hasError</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;"> }}></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$message</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

If you need to merge other attributes onto your component, you can chain the merge
method onto the class method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> {{ </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">class</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: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;"> => </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">button</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><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$slot</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

lightbulb

If you need to conditionally compile classes on other HTML elements that shouldn’t receive
merged attributes, you can use the @class
directive
.

Non-Class Attribute
Merging

When merging attributes that are not class attributes, the values provided to the
merge method will be considered the «default» values of the attribute. However,
unlike the class attribute, these attributes will not be merged with injected
attribute values. Instead, they will be overwritten. For example, a button
component’s implementation may look like the following:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> {{ </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;"> => </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">button</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><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$slot</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

To render the button component with a custom type, it may be specified when
consuming the component. If no type is specified, the button type will be used:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">submit</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Submit</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-button</span><span style="color: #89DDFF;">></span></div>

The rendered HTML of the button component in this example would be:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">submit</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Submit</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">button</span><span style="color: #89DDFF;">></span></div>

If you would like an attribute other than class to have its default value and
injected values joined together, you may use the prepends method. In this example,
the data-controller attribute will always begin with
profile-controller and any additional injected data-controller values
will be placed after this default value:

<!-- 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: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">data-<code>controller' => $attributes->prepends('profile-controller')]) }}>
{{ $slot }}
</spandiv>

Retrieving and Filtering
Attributes

You may filter attributes using the filter method. This method accepts a closure
which should return true if you wish to retain the attribute in the attribute bag:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">fn </span><span style="color: #BFC7D5;">(</span><span style="color: #C792EA;">string</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: #82AAFF;"> </span><span style="color: #C792EA;">string</span><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">$</span><span style="color: #BEC5D4;">key</span><span style="color: #BFC7D5;">) </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</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;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">) </span><span style="color: #89DDFF;">}}</span></div>

For convenience, you may use the whereStartsWith method to retrieve all attributes
whose keys begin with a given string:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereStartsWith</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">wire:<code>model') }}

Conversely, the whereDoesntStartWith method may be used to exclude all attributes
whose keys begin with a given string:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereDoesntStartWith</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">wire:<code>model') }}

Using the first method, you may render the first attribute in a given attribute bag:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereStartsWith</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">wire:<code>model')->first() }}

If you would like to check if an attribute is present on the component, you may use the
has method. This method accepts the attribute name as its only argument and returns
a boolean indicating whether or not the attribute is present:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">class</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Class attribute is present</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

If an array is passed to the has method, the method will determine if all of the
given attributes are present on the component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</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;">class</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">All of the attributes are present</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

The hasAny method may be used to determine if any of the given attributes are
present on the component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasAny</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">href</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">:href</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">v-bind:href</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">One of the attributes is present</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

You may retrieve a specific attribute’s value using the get method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$attributes</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;">class</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">) </span><span style="color: #89DDFF;">}}</span></div>

Reserved Keywords

By default, some keywords are reserved for Blade’s internal use in order to render components.
The following keywords cannot be defined as public properties or method names within your
components:

  • data
  • render
  • resolveView
  • shouldRender
  • view
  • withAttributes
  • withName

Slots

You will often need to pass additional content to your component via «slots». Component slots are
rendered by echoing the $slot variable. To explore this concept, let’s imagine that
an alert component has the following markup:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/components/alert.blade.php -->
 
<div class="alert alert-danger">
{{ $slot }}
</spandiv>

We may pass content to the slot by injecting content into the component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Whoops!</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;"> Something went wrong!</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div>

Sometimes a component may need to render multiple different slots in different locations within
the component. Let’s modify our alert component to allow for the injection of a «title» slot:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/components/alert.blade.php -->
 
<span class="alert-title">{{ $title }}</spanspan>
 
<div class="alert alert-danger">
{{ $slot }}
</spandiv>

You may define the content of the named slot using the x-slot tag. Any content not
within an explicit x-slot tag will be passed to the component in the
$slot variable:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-slot:title</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">        Server Error</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-slot</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;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Whoops!</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;"> Something went wrong!</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div>

You may invoke a slot’s isEmpty method to determine if the slot contains content:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">span</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">alert-title</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$title</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">span</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">alert alert-danger</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$slot</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isEmpty</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;">        This is default content if the slot is empty.</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@else</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$slot</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endif</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Additionally, the hasActualContent method may be used to determine if the slot
contains any «actual» content that is not an HTML comment:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$slot</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasActualContent</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;">    The scope has non-comment content.</span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

Scoped Slots

If you have used a JavaScript framework such as Vue, you may be familiar with «scoped slots»,
which allow you to access data or methods from the component within your slot. You may achieve
similar behavior in Laravel by defining public methods or properties on your component and
accessing the component within your slot via the $component variable. In this
example, we will assume that the x-alert component has a public
formatAlert method defined on its component class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-slot:title</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$component</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">formatAlert</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Server Error</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><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-slot</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;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">Whoops!</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">strong</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;"> Something went wrong!</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">></span></div>

Slot Attributes

Like Blade components, you may assign additional attributes
to slots such as CSS class names:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-card</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">shadow-sm</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-slot:heading</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">font-bold</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">        Heading</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-slot</span><span style="color: #89DDFF;">></span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">    Content</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-slot:footer</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">text-sm</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">        Footer</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-slot</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-card</span><span style="color: #89DDFF;">></span></div>

To interact with slot attributes, you may access the attributes property of the
slot’s variable. For more information on how to interact with attributes, please consult the
documentation on component attributes:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@props</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">heading</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;">footer</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: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> {{ </span><span style="color: #BEC5D4;">$attributes</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">class</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">border</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><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">h1</span><span style="color: #89DDFF;"> {{ </span><span style="color: #BEC5D4;">$heading</span><span style="color: #89DDFF;">->attributes-></span><span style="color: #82AAFF;">class</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">text-lg</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><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$heading</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">h1</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: #BFC7D5;"> </span><span style="color: #BEC5D4;">$slot</span><span style="color: #BFC7D5;"> </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;">footer</span><span style="color: #89DDFF;"> {{ </span><span style="color: #BEC5D4;">$footer</span><span style="color: #89DDFF;">->attributes-></span><span style="color: #82AAFF;">class</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">text-gray-700</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><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$footer</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">footer</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Inline Component Views

For very small components, it may feel cumbersome to manage both the component class and the
component’s view template. For this reason, you may return the component’s markup
directly from the render method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the <code>view / contents that represent the component.
*/
public function render(): string
{
return <<<'blade'
{{ $slot }}
blade;
}

Generating
Inline View Components

To create a component that renders an inline view, you may use the
inline option when executing the make:component command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:component Alert --inline

Dynamic Components

Sometimes you may need to render a component but not know which component should be rendered
until runtime. In this situation, you may use Laravel’s built-in dynamic-component
component to render the component based on a runtime value or variable:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">// $componentName = "secondary-button";</span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-dynamic-component</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:component</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$componentName</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mt-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> /></span></div>

Manually
Registering Components

exclamation

The following documentation on manually registering components is primarily applicable to
those who are writing Laravel packages that include view components. If you are
not writing a package, this portion of the component documentation may not be relevant to
you.

When writing components for your own application, components are automatically discovered within
the app/View/Components directory and resources/views/components
directory.

However, if you are building a package that utilizes Blade components or placing components in
non-conventional directories, you will need to manually register your component class and its
HTML tag alias so that Laravel knows where to find the component. You should typically register
your components in the boot method of your package’s service provider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> VendorPackage\View\Components\</span><span style="color: #FFCB8B;">AlertComponent</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 your package's 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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">component</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">package-alert</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">AlertComponent</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>

Once your component has been registered, it may be rendered using its tag alias:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-package-alert</span><span style="color: #89DDFF;">/></span></div>

Autoloading Package Components

Alternatively, you may use the componentNamespace method to autoload component
classes by convention. For example, a Nightshade package might have
Calendar and ColorPicker components that reside within the
Package\Views\Components namespace:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</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 your package's 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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">componentNamespace</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Nightshade</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Views</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Components</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">nightshade</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

This will allow the usage of package components by their vendor namespace using the
package-name:: syntax:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-nightshade::calendar</span><span style="color: #89DDFF;"> /></span></div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-nightshade::color-picker</span><span style="color: #89DDFF;"> /></span></div>

Blade will automatically detect the class that’s linked to this component by pascal-casing the
component name. Subdirectories are also supported using «dot» notation.

Anonymous Components

Similar to inline components, anonymous components provide a mechanism for managing a component
via a single file. However, anonymous components utilize a single view file and
have no associated class. To define an anonymous component, you only need to place a Blade
template within your resources/views/components directory. For example, assuming
you have defined a component at resources/views/components/alert.blade.php, you may
simply render it like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;">/></span></div>

You may use the . character to indicate if a component is nested deeper inside the
components directory. For example, assuming the component is defined at
resources/views/components/inputs/button.blade.php, you may render it like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-inputs.button</span><span style="color: #89DDFF;">/></span></div>

Anonymous Index
Components

Sometimes, when a component is made up of many Blade templates, you may wish to group the given
component’s templates within a single directory. For example, imagine an «accordion» component
with the following directory structure:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">/resources/<code>views/components/accordion.blade.php
/resources/views/components/accordion/item.blade.php

This directory structure allows you to render the accordion component and its item like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-accordion</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-accordion.item</span><span style="color: #89DDFF;">></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><span style="color: #FF5572;">x-accordion.item</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-accordion</span><span style="color: #89DDFF;">></span></div>

However, in order to render the accordion component via x-accordion, we were forced
to place the «index» accordion component template in the resources/views/components
directory instead of nesting it within the accordion directory with the other
accordion related templates.

Thankfully, Blade allows you to place a file matching the component’s directory name within the
component’s directory itself. When this template exists, it can be rendered as the «root»
element of the component even though it is nested within a directory. So, we can continue to use
the same Blade syntax given in the example above; however, we will adjust our directory
structure like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">/resources/<code>views/components/accordion/accordion.blade.php
/resources/views/components/accordion/item.blade.php

Data Properties /
Attributes

Since anonymous components do not have any associated class, you may wonder how you may
differentiate which data should be passed to the component as variables and which attributes
should be placed in the component’s attribute bag.

You may specify which attributes should be considered data variables using the
@props directive at the top of your component’s Blade template. All other
attributes on the component will be available via the component’s attribute bag. If you wish to
give a data variable a default value, you may specify the variable’s name as the array key and
the default value as the array value:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/components/alert.blade.php -->
 
@props(['type' => 'info', 'message'])
 
<div {{ $attributes->merge(['class' => 'alert alert-'.$type]) }}>
{{ $message }}
</spandiv>

Given the component definition above, we may render the component like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-alert</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">type</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">error</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:message</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$message</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mb-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">/></span></div>

Accessing Parent Data

Sometimes you may want to access data from a parent component inside a child component. In these
cases, you may use the @aware directive. For example, imagine we are building a
complex menu component consisting of a parent <x-menu> and child
<x-menu.item>:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-menu</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">color</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">purple</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-menu.item</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">...</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-menu.item</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-menu.item</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">...</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-menu.item</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-menu</span><span style="color: #89DDFF;">></span></div>

The <x-menu> component may have an implementation like the following:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/components/menu/index.blade.php -->
 
@props(['color' => 'gray'])
 
<ul {{ $attributes->merge(['class' => 'bg-'.$color.'-200']) }}>
{{ $slot }}
</spanul>

Because the color prop was only passed into the parent
(<x-menu>), it won’t be available inside <x-menu.item>.
However, if we use the @aware directive, we can make it available inside
<x-menu.item> as well:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/components/menu/item.blade.php -->
 
@aware(['color' => 'gray'])
 
<li {{ $attributes->merge(['class' => 'text-'.$color.'-800']) }}>
{{ $slot }}
</spanli>

exclamation

The @aware directive cannot access parent data that is not explicitly passed to
the parent component via HTML attributes. Default @props values that are not
explicitly passed to the parent component cannot be accessed by the @aware
directive.

Anonymous Component
Paths

As previously discussed, anonymous components are typically defined by placing a Blade template
within your resources/views/components directory. However, you may occasionally
want to register other anonymous component paths with Laravel in addition to the default path.

The anonymousComponentPath method accepts the «path» to the anonymous component
location as its first argument and an optional «namespace» that components should be placed
under as its second argument. Typically, this method should be called from the boot
method of one of your application’s service providers:

<!-- Syntax highlighted by torchlight.dev --><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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">anonymousComponentPath</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">__DIR__</span><span style="color: #89DDFF;">.</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/../components</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

When component paths are registered without a specified prefix as in the example above, they may
be rendered in your Blade components without a corresponding prefix as well. For example, if a
panel.blade.php component exists in the path registered above, it may be rendered
like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-panel</span><span style="color: #89DDFF;"> /></span></div>

Prefix «namespaces» may be provided as the second argument to the
anonymousComponentPath method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">anonymousComponentPath</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">__DIR__</span><span style="color: #89DDFF;">.</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/../components</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">dashboard</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

When a prefix is provided, components within that «namespace» may be rendered by prefixing to the
component’s namespace to the component name when the component is rendered:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FFFFFF;">x-dashboard::panel</span><span style="color: #89DDFF;"> /></span></div>

Building Layouts

Layouts Using Components

Most web applications maintain the same general layout across various pages. It would be
incredibly cumbersome and hard to maintain our application if we had to repeat the entire layout
HTML in every view we create. Thankfully, it’s convenient to define this layout as
a single Blade component and then use it throughout our application.

Defining the Layout
Component

For example, imagine we are building a «todo» list application. We might define a
layout component that looks like the following:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> resources/<code>views/components/layout.blade.php -->
 
<html>
<head>
<title>{{ $title ?? 'Todo Manager' }}</spantitle>
</spanhead>
<body>
<h1>Todos</spanh1>
<hr/>
{{ $slot }}
</spanbody>
</spanhtml>

Applying the Layout
Component

Once the layout component has been defined, we may create a Blade view
that utilizes the component. In this example, we will define a simple view that
displays our task list:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> resources/<code>views/tasks.blade.php -->
 
<x-layout>
@foreach ($tasks as $task)
<div>{{ $task }}</spandiv>
@endforeach
</spanx-layout>

Remember, content that is injected into a component will be supplied to the default
$slot variable within our layout component. As you may have noticed,
our layout also respects a $title slot if one is provided; otherwise,
a default title is shown. We may inject a custom title from our task list view
using the standard slot syntax discussed in the component
documentation
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> resources/<code>views/tasks.blade.php -->
 
<x-layout>
<x-slot:title>
Custom Title
</spanx-slot>
 
@foreach ($tasks as $task)
<div>{{ $task }}</spandiv>
@endforeach
</spanx-layout>

Now that we have defined our layout and task list views, we just need to return the
task view from a route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Task</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/tasks</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: #82AAFF;"><code>view('tasks', ['tasks' => Task::all()]);
});

Layouts
Using Template Inheritance

Defining a Layout

Layouts may also be created via «template inheritance». This was the primary way of building
applications prior to the introduction of components.

To get started, let’s take a look at a simple example. First, we will examine a page layout.
Since most web applications maintain the same general layout across various pages, it’s
convenient to define this layout as a single Blade view:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> resources/<code>views/layouts/app.blade.php -->
 
<html>
<head>
<title>App Name - @yield('title')</spantitle>
</spanhead>
<body>
@section('sidebar')
This is the master sidebar.
@show
 
<div class="container">
@yield('content')
</spandiv>
</spanbody>
</spanhtml>

As you can see, this file contains typical HTML mark-up. However, take note of the
@section and @yield directives. The @section directive,
as the name implies, defines a section of content, while the @yield directive is
used to display the contents of a given section.

Now that we have defined a layout for our application, let’s define a child page that inherits
the layout.

Extending a Layout

When defining a child view, use the @extends Blade directive to specify
which layout the child view should «inherit». Views which extend a Blade layout may
inject content into the layout’s sections using @section directives. Remember, as
seen in the example above, the contents of these sections will be displayed in the layout using
@yield:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> resources/<code>views/child.blade.php -->
 
@extends('layouts.app')
 
@section('title', 'Page Title')
 
@section('sidebar')
@parent
 
<p>This is appended to the master sidebar.</spanp>
@endsection
 
@section('content')
<p>This is my body content.</spanp>
@endsection

In this example, the sidebar section is utilizing the @parent directive
to append (rather than overwriting) content to the layout’s sidebar. The @parent
directive will be replaced by the content of the layout when the view is rendered.

lightbulb

Contrary to the previous example, this sidebar section ends with
@endsection instead of @show. The @endsection
directive will only define a section while @show will define and
immediately yield the section.

The @yield directive also accepts a default value as its second parameter. This
value will be rendered if the section being yielded is undefined:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@yield</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">content</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Default content</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div>

Forms

CSRF Field

Anytime you define an HTML form in your application, you should include a hidden CSRF token field
in the form so that the CSRF protection middleware can
validate the request. You may use the @csrf Blade directive to generate the token
field:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">method</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">POST</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">action</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">/profile</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@csrf</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">    ...</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;">></span></div>

Method Field

Since HTML forms can’t make PUT, PATCH, or DELETE
requests, you will need to add a hidden _method field to spoof these HTTP verbs.
The @method Blade directive can create this field for you:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">action</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">/foo/bar</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">method</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">POST</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@method</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">PUT</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">    ...</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">form</span><span style="color: #89DDFF;">></span></div>

Validation Errors

The @error directive may be used to quickly check if validation error messages
exist for a given attribute. Within an @error directive, you may echo the
$message variable to display the error message:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/post/create.blade.php -->
 
<label for="title">Post Title</spanlabel>
 
<input
id="title"
type="text"
class="@error('title') is-invalid @enderror"
/>
 
@error('title')
<div class="alert alert-danger">{{ $message }}</spandiv>
@enderror

Since the @error directive compiles to an «if» statement, you may use the
@else directive to render content when there is not an error for an attribute:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/auth.blade.php -->
 
<label for="email">Email address</spanlabel>
 
<input
id="email"
type="email"
class="@error('email') is-invalid @else is-valid @enderror"
/>

You may pass the name of a specific error bag as
the second parameter to the @error directive to retrieve validation error messages
on pages containing multiple forms:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> /resources/<code>views/auth.blade.php -->
 
<label for="email">Email address</spanlabel>
 
<input
id="email"
type="email"
class="@error('email', 'login') is-invalid @enderror"
/>
 
@error('email', 'login')
<div class="alert alert-danger">{{ $message }}</spandiv>
@enderror

Stacks

Blade allows you to push to named stacks which can be rendered somewhere else in another
view or layout. This can be particularly useful for specifying any JavaScript
libraries required by your child views:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">src</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">/example.js</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endpush</span></div>

If you would like to @push content if a given boolean expression evaluates to
true, you may use the @pushIf directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@pushIf</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$shouldPush</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">src</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">/example.js</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span</span><span style="color: #FF5572;">script</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #82AAFF;">@endPushIf</span></div>

You may push to a stack as many times as needed. To render the complete stack contents, pass the
name of the stack to the @stack directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">head</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> Head Contents </span><span style="color: #697098;">--></span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@stack</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">head</span><span style="color: #89DDFF;">></span></div>

If you would like to prepend content onto the beginning of a stack, you should use the
@prepend directive:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">    This will be second...</span></div><div class="line"><span style="color: #C792EA;">@endpush</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">// Later...</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">scripts</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">    This will be first...</span></div><div class="line"><span style="color: #C792EA;">@endprepend</span></div>

Service Injection

The @inject directive may be used to retrieve a service from the Laravel service container. The first argument passed to
@inject is the name of the variable the service will be placed into, while the
second argument is the class or interface name of the service you wish to resolve:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@inject</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">metrics</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">App\Services\MetricsService</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"> </div><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Monthly Revenue: </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$metrics</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">monthlyRevenue</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;">.</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div>

Rendering
Inline Blade Templates

Sometimes you may need to transform a raw Blade template string into valid HTML. You may
accomplish this using the render method provided by the Blade facade.
The render method accepts the Blade template string and an optional array of data
to provide to the template:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</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: #FFCB8B;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">render</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Hello, {{ $name }}</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;">Julian Bashir</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div>

Laravel renders inline Blade templates by writing them to the
storage/framework/views directory. If you would like Laravel to remove these
temporary files after rendering the Blade template, you may provide the
deleteCachedView argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">render</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Hello, {{ $name }}</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;">Julian Bashir</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">    deleteCachedView: </span><span style="color: #82AAFF;">true</span></div><div class="line"><span style="color: #BFC7D5;">);</span></div>

Rendering Blade
Fragments

When using frontend frameworks such as Turbo and htmx, you may occasionally need to only return a portion of a
Blade template within your HTTP response. Blade «fragments» allow you to do just that. To get
started, place a portion of your Blade template within @fragment and
@endfragment directives:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@fragment</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">user-list</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #82AAFF;">@endfragment</span></div>

Then, when rendering the view that utilizes this template, you may invoke the
fragment method to specify that only the specified fragment should be included in
the outgoing HTTP response:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;"><code>view('dashboard', ['users' => $users])->fragment('user-list');

The fragmentIf method allows you to conditionally return a fragment of a
view based on a given condition. Otherwise, the entire view will be
returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;"><code>view('dashboard', ['users' => $users])
->fragmentIf($request->hasHeader('HX-Request'), 'user-list');

The fragments and fragmentsIf methods allow you to return multiple
view fragments in the response. The fragments will be concatenated together:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;"><code>view('dashboard', ['users' => $users])
->fragments(['user-list', 'comment-list']);
 
view('dashboard', ['users' => $users])
->fragmentsIf(
$request->hasHeader('HX-Request'),
['user-list', 'comment-list']
);

Extending Blade

Blade allows you to define your own custom directives using the directive method.
When the Blade compiler encounters the custom directive, it will call the provided callback with
the expression that the directive contains.

The following example creates a @datetime($var) directive which formats a given
$var, which should be an instance of DateTime:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Providers;
 
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// ...
}
 
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::directive('datetime', function (string $expression) {
return "php echo (</span$expression)->format('m/d/Y H:i'); ?>";
});
}
}

As you can see, we will chain the format method onto whatever expression is passed
into the directive. So, in this example, the final PHP generated by this directive will be:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span echo ($var)->format('m/d/Y H:i'); ?>
exclamation

After updating the logic of a Blade directive, you will need to delete all of the cached
Blade views. The cached Blade views may be removed using the
view:clear Artisan command.

Custom Echo Handlers

If you attempt to «echo» an object using Blade, the object’s __toString method will
be invoked. The __toString
method is one of PHP’s built-in «magic methods». However, sometimes you may not have control
over the __toString method of a given class, such as when the class that you are
interacting with belongs to a third-party library.

In these cases, Blade allows you to register a custom echo handler for that particular type of
object. To accomplish this, you should invoke Blade’s stringable method. The
stringable method accepts a closure. This closure should type-hint the type of
object that it is responsible for rendering. Typically, the stringable method
should be invoked within the boot method of your application’s
AppServiceProvider class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Money\</span><span style="color: #FFCB8B;">Money</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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">stringable</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;">Money</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$money</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;">$money</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">formatTo</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">en_GB</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>

Once your custom echo handler has been defined, you may simply echo the object in your Blade
template:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Cost: </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$money</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span></div>

Custom If Statements

Programming a custom directive is sometimes more complex than necessary when defining simple,
custom conditional statements. For that reason, Blade provides a Blade::if method
which allows you to quickly define custom conditional directives using closures. For example,
let’s define a custom conditional that checks the configured default «disk» for the
application. We may do this in the boot method of our
AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Blade</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;">Blade</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">if</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">disk</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: #C792EA;">string</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: #82AAFF;"><code>config('filesystems.default') === $value;
});
}

Once the custom conditional has been defined, you can use it within your templates:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">@disk</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">local</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><span style="color: #697098;"> The application is using the local disk... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #82AAFF;">@elsedisk</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">s3</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><span style="color: #697098;"> The application is using the s3 disk... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #C792EA;">@else</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;"><!--</span></span><span style="color: #697098;"> The application is using some other disk... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #82AAFF;">@enddisk</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">@unlessdisk</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">local</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><span style="color: #697098;"> The application is not using the local disk... </span><span style="color: #697098;">--></span></div><div class="line"><span style="color: #82AAFF;">@enddisk</span></div>



Broadcasting

Broadcasting

Introduction

In many modern web applications, WebSockets are used to implement realtime, live-updating user
interfaces. When some data is updated on the server, a message is typically sent over a
WebSocket connection to be handled by the client. WebSockets provide a more efficient
alternative to continually polling your application’s server for data changes that should be
reflected in your UI.

For example, imagine your application is able to export a user’s data to a CSV file and email it
to them. However, creating this CSV file takes several minutes so you choose to create and mail
the CSV within a queued job. When the CSV has been created and mailed
to the user, we can use event broadcasting to dispatch an
App\Events\UserDataExported event that is received by our application’s JavaScript.
Once the event is received, we can display a message to the user that their CSV has been emailed
to them without them ever needing to refresh the page.

To assist you in building these types of features, Laravel makes it easy to «broadcast» your
server-side Laravel events over a WebSocket connection. Broadcasting
your Laravel events allows you to share the same event names and data between your server-side
Laravel application and your client-side JavaScript application.

The core concepts behind broadcasting are simple: clients connect to named channels on the
frontend, while your Laravel application broadcasts events to these channels on the backend.
These events can contain any additional data you wish to make available to the frontend.

Supported Drivers

By default, Laravel includes three server-side broadcasting drivers for you to choose from: Laravel Reverb, Pusher Channels, and Ably.

lightbulb

Before diving into event broadcasting, make sure you have read Laravel’s documentation on events and listeners.

Server Side Installation

To get started using Laravel’s event broadcasting, we need to do some configuration
within the Laravel application as well as install a few packages.

Event broadcasting is accomplished by a server-side broadcasting driver that broadcasts your
Laravel events so that Laravel Echo (a JavaScript library) can receive them within the browser
client. Don’t worry – we’ll walk through each part of the installation process step-by-step.

Configuration

All of your application’s event broadcasting configuration is stored in the
config/broadcasting.php configuration file. Don’t worry if this
directory does not exist in your application; it will be created when you run the
install:broadcasting Artisan command.

Laravel supports several broadcast drivers out of the box: Laravel
Reverb
, Pusher Channels, Ably, and a log driver for local development and
debugging. Additionally, a null driver is included which allows you to disable
broadcasting during testing. A configuration example is included for each of these
drivers in the config/broadcasting.php configuration file.

Installation

By default, broadcasting is not enabled in new Laravel applications. You may enable broadcasting
using the install:broadcasting Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan install:broadcasting

The install:broadcasting command will create the
config/broadcasting.php configuration file. In addition, the command
will create the routes/channels.php file where you may register your application’s
broadcast authorization routes and callbacks.

Queue Configuration

Before broadcasting any events, you should first configure and run a queue worker. All event broadcasting is done via queued jobs so that
the response time of your application is not seriously affected by events being broadcast.

Reverb

When running the install:broadcasting command, you will be prompted to install Laravel Reverb. Of course, you may also install Reverb manually using
the Composer package manager.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require laravel/reverb

Once the package is installed, you may run Reverb’s installation command to publish the
configuration, add Reverb’s required environment variables, and enable
event broadcasting in your application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan reverb:install

You can find detailed Reverb installation and usage instructions in the Reverb documentation.

Pusher Channels

If you plan to broadcast your events using Pusher
Channels
, you should install the Pusher Channels PHP SDK using the Composer package
manager:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require pusher/pusher-php-server

Next, you should configure your Pusher Channels credentials in the
config/broadcasting.php configuration file. An example Pusher Channels
configuration is already included in this file, allowing you to quickly specify
your key, secret, and application ID. Typically, you should configure your Pusher
Channels credentials in your application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">PUSHER_APP_ID</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-app-id</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_KEY</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-key</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_SECRET</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-secret</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_HOST</span><span style="color: #BFC7D5;">=</span></div><div class="line"><span style="color: #C792EA;">PUSHER_PORT</span><span style="color: #BFC7D5;">=443</span></div><div class="line"><span style="color: #C792EA;">PUSHER_SCHEME</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">https</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_CLUSTER</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mt1</span><span style="color: #D9F5DD;">"</span></div>

The config/broadcasting.php file’s pusher configuration
also allows you to specify additional options that are supported by Channels, such
as the cluster.

Then, set the BROADCAST_CONNECTION environment variable to
pusher in your application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">BROADCAST_CONNECTION</span><span style="color: #BFC7D5;">=pusher</span></div>

Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events
on the client-side.

Ably

lightbulb

The documentation below discusses how to use Ably in «Pusher compatibility» mode. However,
the Ably team recommends and maintains a broadcaster and Echo client that is able to take
advantage of the unique capabilities offered by Ably. For more information on using the Ably
maintained drivers, please consult
Ably’s Laravel broadcaster documentation
.

If you plan to broadcast your events using Ably, you should
install the Ably PHP SDK using the Composer package manager:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require ably/ably-php

Next, you should configure your Ably credentials in the
config/broadcasting.php configuration file. An example Ably
configuration is already included in this file, allowing you to quickly specify
your key. Typically, this value should be set via the ABLY_KEY environment variable:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">ABLY_KEY</span><span style="color: #BFC7D5;">=your-ably-key</span></div>

Then, set the BROADCAST_CONNECTION environment variable to
ably in your application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">BROADCAST_CONNECTION</span><span style="color: #BFC7D5;">=ably</span></div>

Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events
on the client-side.

Client Side Installation

Reverb

Laravel Echo is a JavaScript library that makes it
painless to subscribe to channels and listen for events broadcast by your server-side
broadcasting driver. You may install Echo via the NPM package manager. In this example, we will
also install the pusher-js package since Reverb utilizes the Pusher protocol for
WebSocket subscriptions, channels, and messages:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm install --save-dev laravel-echo pusher-js

Once Echo is installed, you are ready to create a fresh Echo instance in your application’s
JavaScript. A great place to do this is at the bottom of the
resources/js/bootstrap.js file that is included with the Laravel framework. By
default, an example Echo configuration is already included in this file – you
simply need to uncomment it and update the broadcaster configuration
option to reverb:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">import</span><span style="color: #BFC7D5;"> Echo </span><span style="color: #C792EA;">from</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel-echo';
 
import Pusher from 'pusher-js';
window.Pusher = Pusher;
 
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT,
wssPort: import.meta.env.VITE_REVERB_PORT,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});

Next, you should compile your application’s assets:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm run build

exclamation

The Laravel Echo reverb broadcaster requires laravel-echo
v1.16.0+.

Pusher Channels

Laravel Echo is a JavaScript library that makes it
painless to subscribe to channels and listen for events broadcast by your server-side
broadcasting driver. Echo also leverages the pusher-js NPM package to implement the
Pusher protocol for WebSocket subscriptions, channels, and messages.

The install:broadcasting Artisan command automatically installs the
laravel-echo and pusher-js packages for you; however, you may also
install these packages manually via NPM:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm install --save-dev laravel-echo pusher-js

Once Echo is installed, you are ready to create a fresh Echo instance in your application’s
JavaScript. The install:broadcasting command creates an Echo
configuration file at resources/js/echo.js; however, the default
configuration in this file is intended for Laravel Reverb. You may copy the
configuration below to transition your configuration to Pusher:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">import</span><span style="color: #BFC7D5;"> Echo </span><span style="color: #C792EA;">from</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel-echo';
 
import Pusher from 'pusher-js';
window.Pusher = Pusher;
 
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
forceTLS: true
});

Next, you should define the appropriate values for the Pusher environment variables
in your application’s .env file. If these variables do not already exist in your
.env file, you should add them:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">PUSHER_APP_ID</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-app-id</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_KEY</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-key</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_SECRET</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-pusher-secret</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_HOST</span><span style="color: #BFC7D5;">=</span></div><div class="line"><span style="color: #C792EA;">PUSHER_PORT</span><span style="color: #BFC7D5;">=443</span></div><div class="line"><span style="color: #C792EA;">PUSHER_SCHEME</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">https</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PUSHER_APP_CLUSTER</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">mt1</span><span style="color: #D9F5DD;">"</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">VITE_APP_NAME</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${APP_NAME}</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">VITE_PUSHER_APP_KEY</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${PUSHER_APP_KEY}</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">VITE_PUSHER_HOST</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${PUSHER_HOST}</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">VITE_PUSHER_PORT</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${PUSHER_PORT}</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">VITE_PUSHER_SCHEME</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${PUSHER_SCHEME}</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">VITE_PUSHER_APP_CLUSTER</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">${PUSHER_APP_CLUSTER}</span><span style="color: #D9F5DD;">"</span></div>

Once you have adjusted the Echo configuration according to your application’s needs,
you may compile your application’s assets:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm run build

lightbulb

To learn more about compiling your application’s JavaScript assets, please consult the
documentation on Vite.

Using an
Existing Client Instance

If you already have a pre-configured Pusher Channels client instance that you would
like Echo to utilize, you may pass it to Echo via the client
configuration option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">import</span><span style="color: #BFC7D5;"> Echo </span><span style="color: #C792EA;">from</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel-echo';
import Pusher from 'pusher-js';
 
const options = {
broadcaster: 'pusher',
key: 'your-pusher-channels-key'
}
 
window.Echo = new Echo({
...options,
client: new Pusher(options.key, options)
});

Ably

lightbulb

The documentation below discusses how to use Ably in «Pusher compatibility» mode. However,
the Ably team recommends and maintains a broadcaster and Echo client that is able to take
advantage of the unique capabilities offered by Ably. For more information on using the Ably
maintained drivers, please consult
Ably’s Laravel broadcaster documentation
.

Laravel Echo is a JavaScript library that makes it
painless to subscribe to channels and listen for events broadcast by your server-side
broadcasting driver. Echo also leverages the pusher-js NPM package to implement the
Pusher protocol for WebSocket subscriptions, channels, and messages.

The install:broadcasting Artisan command automatically installs the
laravel-echo and pusher-js packages for you; however, you may also
install these packages manually via NPM:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm install --save-dev laravel-echo pusher-js

Before continuing, you should enable Pusher protocol support in your Ably application
settings. You may enable this feature within the «Protocol Adapter Settings» portion of your
Ably application’s settings dashboard.

Once Echo is installed, you are ready to create a fresh Echo instance in your application’s
JavaScript. The install:broadcasting command creates an Echo
configuration file at resources/js/echo.js; however, the default
configuration in this file is intended for Laravel Reverb. You may copy the
configuration below to transition your configuration to Ably:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">import</span><span style="color: #BFC7D5;"> Echo </span><span style="color: #C792EA;">from</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>laravel-echo';
 
import Pusher from 'pusher-js';
window.Pusher = Pusher;
 
window.Echo = new Echo({
broadcaster: 'pusher',
key: import.meta.env.VITE_ABLY_PUBLIC_KEY,
wsHost: 'realtime-pusher.ably.io',
wsPort: 443,
disableStats: true,
encrypted: true,
});

You may have noticed our Ably Echo configuration references a
VITE_ABLY_PUBLIC_KEY environment variable. This variable’s value
should be your Ably public key. Your public key is the portion of your Ably key that occurs
before the : character.

Once you have adjusted the Echo configuration according to your needs, you may
compile your application’s assets:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>npm run dev

lightbulb

To learn more about compiling your application’s JavaScript assets, please consult the
documentation on Vite.

Concept Overview

Laravel’s event broadcasting allows you to broadcast your server-side Laravel events to your
client-side JavaScript application using a driver-based approach to WebSockets. Currently,
Laravel ships with Pusher Channels and Ably drivers. The events may be easily consumed on the
client-side using the Laravel Echo JavaScript package.

Events are broadcast over «channels», which may be specified as public or private. Any visitor to
your application may subscribe to a public channel without any authentication or authorization;
however, in order to subscribe to a private channel, a user must be authenticated and authorized
to listen on that channel.

Using an Example
Application

Before diving into each component of event broadcasting, let’s take a high level
overview using an e-commerce store as an example.

In our application, let’s assume we have a page that allows users to view the
shipping status for their orders. Let’s also assume that an
OrderShipmentStatusUpdated event is fired when a shipping status update is
processed by the application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Events\</span><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dispatch</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;">);</span></div>

The
ShouldBroadcast Interface

When a user is viewing one of their orders, we don’t want them to have to refresh
the page to view status updates. Instead, we want to broadcast the updates to the
application as they are created. So, we need to mark the OrderShipmentStatusUpdated
event with the ShouldBroadcast interface. This will instruct Laravel to broadcast
the event when it is fired:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Events;
 
use App\Models\Order;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
 
class OrderShipmentStatusUpdated implements ShouldBroadcast
{
/**
* The order instance.
*
* @var \App\Models\Order
*/
public $order;
}

The ShouldBroadcast interface requires our event to define a
broadcastOn method. This method is responsible for returning the channels that the
event should broadcast on. An empty stub of this method is already defined on generated event
classes, so we only need to fill in its details. We only want the creator of the order to be
able to view status updates, so we will broadcast the event on a private channel
that is tied to the order:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Broadcasting\</span><span style="color: #FFCB8B;">Channel</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Broadcasting\</span><span style="color: #FFCB8B;">PrivateChannel</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;"> * Get the channel the event should broadcast on.</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;">broadcastOn</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Channel</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: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">PrivateChannel</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: #89DDFF;">.</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->order->id</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you wish the event to broadcast on multiple channels, you may return an array
instead:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Broadcasting\</span><span style="color: #FFCB8B;">PrivateChannel</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;"> * Get the channels the event should broadcast on.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">int</span><span style="color: #697098;">, \Illuminate\Broadcasting\Channel></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;">broadcastOn</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">PrivateChannel</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: #89DDFF;">.</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->order->id</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><div class="line"><span style="color: #BFC7D5;">}</span></div>

Authorizing Channels

Remember, users must be authorized to listen on private channels. We may define our channel
authorization rules in our application’s routes/channels.php file. In this example,
we need to verify that any user attempting to listen on the private orders.1
channel is actually the creator of the order:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Order</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"> </div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orders.{orderId}</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$orderId</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;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findOrNew</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">->user_id</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

The channel method accepts two arguments: the name of the channel and a callback
which returns true or false indicating whether the user is authorized
to listen on the channel.

All authorization callbacks receive the currently authenticated user as their first argument and
any additional wildcard parameters as their subsequent arguments. In this example, we are using
the {orderId} placeholder to indicate that the «ID» portion of the channel name is
a wildcard.

Listening for
Event Broadcasts

Next, all that remains is to listen for the event in our JavaScript application. We can do this
using Laravel Echo. First, we’ll use the
private method to subscribe to the private channel. Then, we may use the
listen method to listen for the OrderShipmentStatusUpdated event. By
default, all of the event’s public properties will be included on the broadcast event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">orderId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">OrderShipmentStatusUpdated</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(e</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

Defining Broadcast
Events

To inform Laravel that a given event should be broadcast, you must implement the
Illuminate\Contracts\Broadcasting\ShouldBroadcast interface on the event class.
This interface is already imported into all event classes generated by the framework so you may
easily add it to any of your events.

The ShouldBroadcast interface requires you to implement a single method:
broadcastOn. The broadcastOn method should return a channel or array
of channels that the event should broadcast on. The channels should be instances of
Channel, PrivateChannel, or PresenceChannel. Instances of
Channel represent public channels that any user may subscribe to, while
PrivateChannels and PresenceChannels represent private channels that
require channel authorization:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Events;
 
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
 
class ServerCreated implements ShouldBroadcast
{
use SerializesModels;
 
/**
* Create a new event instance.
*/
public function __construct(
public User $user,
) {}
 
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('user.'.$this->user->id),
];
}
}

After implementing the ShouldBroadcast interface, you only need to fire the event as you normally would. Once the event has been fired,
a queued job will automatically broadcast the event using your
specified broadcast driver.

Broadcast Name

By default, Laravel will broadcast the event using the event’s class name. However, you may
customize the broadcast name by defining a broadcastAs method on the event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The event's broadcast name.</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;">broadcastAs</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</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: #D9F5DD;">'</span><span style="color: #C3E88D;">server.created</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you customize the broadcast name using the broadcastAs method, you should make
sure to register your listener with a leading . character. This will instruct Echo
to not prepend the application’s namespace to the event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">.server.created</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;">e</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: #89DDFF;">...</span><span style="color: #89DDFF;">.</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Broadcast Data

When an event is broadcast, all of its public properties are automatically
serialized and broadcast as the event’s payload, allowing you to access any of its public data
from your JavaScript application. So, for example, if your event has a single public
$user property that contains an Eloquent model, the event’s broadcast
payload would be:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"user"</span><span style="color: #BFC7D5;">: {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"id"</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: #C3E88D;">"name"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">Patrick Stewart</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFFFFF;">...</span></div><div class="line"><span style="color: #BFC7D5;">    }</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

However, if you wish to have more fine-grained control over your broadcast payload, you may add a
broadcastWith method to your event. This method should return the array of data
that you wish to broadcast as the event payload:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the data to broadcast.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">string</span><span style="color: #697098;">, mixed></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;">broadcastWith</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: #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: #FF5572;">$this</span><span style="color: #89DDFF;">->user->id</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Broadcast Queue

By default, each broadcast event is placed on the default queue for the default queue connection
specified in your queue.php configuration file. You may customize the
queue connection and name used by the broadcaster by defining connection and
queue properties on your event class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name of the queue connection to use when broadcasting the event.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@var</span><span style="color: #697098;"> </span><span style="color: #C792EA;">string</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: #BEC5D4;">$connection</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;"><code>redis';
 
/**
* The name of the queue on which to place the broadcasting job.
*
* @var string
*/
public $queue = 'default';

Alternatively, you may customize the queue name by defining a broadcastQueue method
on your event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The name of the queue on which to place the broadcasting job.</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;">broadcastQueue</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</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: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you would like to broadcast your event using the sync queue instead of the
default queue driver, you can implement the ShouldBroadcastNow interface instead of
ShouldBroadcast:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
 
class OrderShipmentStatusUpdated implements ShouldBroadcastNow
{
// ...
}

Broadcast Conditions

Sometimes you want to broadcast your event only if a given condition is true. You may define
these conditions by adding a broadcastWhen method to your event class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Determine if this event should broadcast.</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;">broadcastWhen</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">bool</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: #FF5572;">$this</span><span style="color: #89DDFF;">->order->value</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></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Broadcasting and Database Transactions

When broadcast events are dispatched within database transactions, they may be
processed by the queue before the database transaction has committed. When this
happens, any updates you have made to models or database records
during the database transaction may not yet be reflected in the
database. In addition, any models or database records
created within the transaction may not exist in the database. If your event depends
on these models, unexpected errors can occur when the job that broadcasts the event
is processed.

If your queue connection’s after_commit configuration option is set to
false, you may still indicate that a particular broadcast event should be
dispatched after all open database transactions have been committed by implementing
the ShouldDispatchAfterCommit interface on the event class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Events;
 
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
use Illuminate\Queue\SerializesModels;
 
class ServerCreated implements ShouldBroadcast, ShouldDispatchAfterCommit
{
use SerializesModels;
}
lightbulb

To learn more about working around these issues, please review the
documentation regarding queued jobs and
database transactions
.

Authorizing Channels

Private channels require you to authorize that the currently authenticated user can actually
listen on the channel. This is accomplished by making an HTTP request to your Laravel
application with the channel name and allowing your application to determine if the user can
listen on that channel. When using Laravel Echo, the
HTTP request to authorize subscriptions to private channels will be made automatically.

When broadcasting is enabled, Laravel automatically registers the /broadcasting/auth
route to handle authorization requests. The /broadcasting/auth
route is automatically placed within the web middleware
group.

Defining
Authorization Callbacks

Next, we need to define the logic that will actually determine if the currently authenticated
user can listen to a given channel. This is done in the routes/channels.php file
that was created by the install:broadcasting Artisan command. In this file, you may
use the Broadcast::channel method to register channel authorization callbacks:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orders.{orderId}</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$orderId</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;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findOrNew</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">->user_id</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

The channel method accepts two arguments: the name of the channel and a callback
which returns true or false indicating whether the user is authorized
to listen on the channel.

All authorization callbacks receive the currently authenticated user as their first argument and
any additional wildcard parameters as their subsequent arguments. In this example, we are using
the {orderId} placeholder to indicate that the «ID» portion of the channel name is
a wildcard.

You may view a list of your application’s broadcast authorization callbacks using
the channel:list Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan channel:list

Authorization Callback Model Binding

Just like HTTP routes, channel routes may also take advantage of
implicit and explicit route
model binding
. For example, instead of receiving a string or numeric order
ID, you may request an actual Order model instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Order</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"> </div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orders.{order}</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Order</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</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;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">===</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->user_id</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
exclamation

Unlike HTTP route model binding, channel model
binding does not support automatic implicit model binding
scoping
. However, this is rarely a problem because most channels can be scoped based
on a single model‘s unique, primary key.

Authorization Callback Authentication

Private and presence broadcast channels authenticate the current user via your application’s
default authentication guard. If the user is not authenticated, channel authorization is
automatically denied and the authorization callback is never executed. However, you may assign
multiple, custom guards that should authenticate the incoming request if necessary:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">channel</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">guards</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;">web</span><span style="color: #D9F5DD;">'</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></div>

Defining Channel Classes

If your application is consuming many different channels, your routes/channels.php
file could become bulky. So, instead of using closures to authorize channels, you may use
channel classes. To generate a channel class, use the make:channel Artisan command.
This command will place a new channel class in the App/Broadcasting directory.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:channel OrderChannel

Next, register your channel in your routes/channels.php file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Broadcasting\</span><span style="color: #FFCB8B;">OrderChannel</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">orders.{order}</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">OrderChannel</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">);</span></div>

Finally, you may place the authorization logic for your channel in the channel class’
join method. This join method will house the same logic you would have
typically placed in your channel authorization closure. You may also take advantage of channel
model binding:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Broadcasting;
 
use App\Models\Order;
use App\Models\User;
 
class OrderChannel
{
/**
* Create a new channel instance.
*/
public function __construct() {}
 
/**
* Authenticate the user's access to the channel.
*/
public function join(User $user, Order $order): array|bool
{
return $user->id === $order->user_id;
}
}
lightbulb

Like many other classes in Laravel, channel classes will automatically be resolved by the service container. So, you may type-hint any dependencies
required by your channel in its constructor.

Broadcasting Events

Once you have defined an event and marked it with the ShouldBroadcast interface, you
only need to fire the event using the event’s dispatch method. The event dispatcher will notice
that the event is marked with the ShouldBroadcast interface and will queue the
event for broadcasting:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Events\</span><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dispatch</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;">);</span></div>

Only to Others

When building an application that utilizes event broadcasting, you may occasionally need to
broadcast an event to all subscribers to a given channel except for the current user. You may
accomplish this using the broadcast helper and the toOthers method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Events\</span><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</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;">OrderShipmentStatusUpdated</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">update</span><span style="color: #BFC7D5;">))</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toOthers</span><span style="color: #BFC7D5;">();</span></div>

To better understand when you may want to use the toOthers method, let’s imagine a
task list application where a user may create a new task by entering a task name. To create a
task, your application might make a request to a /task URL which broadcasts the
task’s creation and returns a JSON representation of the new task. When your JavaScript
application receives the response from the end-point, it might directly insert the new task into
its task list like so:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">axios</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">post</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/task</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, task)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">then</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">response</span><span style="color: #D9F5DD;">)</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: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">tasks</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(response</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">data</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

However, remember that we also broadcast the task’s creation. If your JavaScript application is
also listening for this event in order to add tasks to the task list, you will have duplicate
tasks in your list: one from the end-point and one from the broadcast. You may solve this by
using the toOthers method to instruct the broadcaster to not broadcast the event to
the current user.

exclamation

Your event must use the Illuminate\Broadcasting\InteractsWithSockets trait in
order to call the toOthers method.

Configuration

When you initialize a Laravel Echo instance, a socket ID is assigned to the connection. If you
are using a global Axios instance to make HTTP
requests from your JavaScript application, the socket ID will automatically be attached to every
outgoing request as an X-Socket-ID header. Then, when you call the
toOthers method, Laravel will extract the socket ID from the header and instruct
the broadcaster to not broadcast to any connections with that socket ID.

If you are not using a global Axios instance, you will need to manually configure
your JavaScript application to send the X-Socket-ID header with all outgoing
requests. You may retrieve the socket ID using the Echo.socketId method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">var</span><span style="color: #BFC7D5;"> socketId </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">socketId</span><span style="color: #BFC7D5;">();</span></div>

Customizing the
Connection

If your application interacts with multiple broadcast connections and you want to broadcast an
event using a broadcaster other than your default, you may specify which connection to push an
event to using the via method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Events\</span><span style="color: #FFCB8B;">OrderShipmentStatusUpdated</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;">OrderShipmentStatusUpdated</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">update</span><span style="color: #BFC7D5;">))</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">via</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pusher</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Alternatively, you may specify the event’s broadcast connection by calling the
broadcastVia method within the event’s constructor. However, before doing so, you
should ensure that the event class uses the InteractsWithBroadcasting trait:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Events;
 
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithBroadcasting;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
 
class OrderShipmentStatusUpdated implements ShouldBroadcast
{
use InteractsWithBroadcasting;
 
/**
* Create a new event instance.
*/
public function __construct()
{
$this->broadcastVia('pusher');
}
}

Anonymous Events

Sometimes, you may want to broadcast a simple event to your application’s frontend without
creating a dedicated event class. To accommodate this, the Broadcast facade allows
you to broadcast «anonymous events»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">on</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: #89DDFF;">.</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">();</span></div>

The example above will broadcast the following event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"event"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">AnonymousEvent</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"data"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">[]</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"channel"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">orders.1</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Using the as and with methods, you may customize the event’s name and
data:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">on</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: #89DDFF;">.</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">as</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">OrderPlaced</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">with</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">();</span></div>

The example above will broadcast an event like the following:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"event"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">OrderPlaced</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"data"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">{ id: 1, total: 100 }</span><span style="color: #D9F5DD;">"</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"channel"</span><span style="color: #BFC7D5;">: </span><span style="color: #D9F5DD;">"</span><span style="color: #80CBC4;">orders.1</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you would like to broadcast the anonymous event on a private or presence channel, you may
utilize the private and presence methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">private</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: #89DDFF;">.</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">presence</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">channels.</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$channel</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">();</span></div>

Broadcasting an anonymous event using the send method dispatches the event to your
application’s queue for processing. However, if you would like to
broadcast the event immediately, you may use the sendNow method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">on</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: #89DDFF;">.</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sendNow</span><span style="color: #BFC7D5;">();</span></div>

To broadcast the event to all channel subscribers except the currently authenticated user, you
can invoke the toOthers method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">on</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: #89DDFF;">.</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toOthers</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">send</span><span style="color: #BFC7D5;">();</span></div>

Receiving Broadcasts

Listening for Events

Once you have installed and instantiated Laravel Echo,
you are ready to start listening for events that are broadcast from your Laravel application.
First, use the channel method to retrieve an instance of a channel, then call the
listen method to listen for a specified event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">OrderShipmentStatusUpdated</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(e</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">name</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

If you would like to listen for events on a private channel, use the private method
instead. You may continue to chain calls to the listen method to listen for
multiple events on a single channel:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</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>

Stop Listening for
Events

If you would like to stop listening to a given event without leaving
the channel
, you may use the stopListening method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">stopListening</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">OrderShipmentStatusUpdated</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div>

Leaving a Channel

To leave a channel, you may call the leaveChannel method on your Echo instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">leaveChannel</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">);</span></div>

If you would like to leave a channel and also its associated private and presence channels, you
may call the leave method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">leave</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">orders.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">order</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">);</span></div>

Namespaces

You may have noticed in the examples above that we did not specify the full
App\Events namespace for the event classes. This is because Echo will automatically
assume the events are located in the App\Events namespace. However, you may
configure the root namespace when you instantiate Echo by passing a
namespace configuration option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">window</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">Echo</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: #82AAFF;">Echo</span><span style="color: #BFC7D5;">({</span></div><div class="line"><span style="color: #BFC7D5;">    broadcaster: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pusher</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;">    namespace: </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">App.Other.Namespace</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Alternatively, you may prefix event classes with a . when subscribing to them using
Echo. This will allow you to always specify the fully-qualified class name:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">channel</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">.Namespace</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Event</span><span style="color: #F78C6C;">\\</span><span style="color: #C3E88D;">Class</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

Presence Channels

Presence channels build on the security of private channels while exposing the additional feature
of awareness of who is subscribed to the channel. This makes it easy to build powerful,
collaborative application features such as notifying users when another user is
viewing the same page or listing the inhabitants of a chat room.

Authorizing Presence
Channels

All presence channels are also private channels; therefore, users must be authorized to access them. However, when defining
authorization callbacks for presence channels, you will not return true if the user
is authorized to join the channel. Instead, you should return an array of data about the user.

The data returned by the authorization callback will be made available to the presence channel
event listeners in your JavaScript application. If the user is not authorized to join the
presence channel, you should return false or null:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Broadcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">channel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">chat.{roomId}</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$roomId</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canJoinRoom</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$roomId</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: #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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</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: #BEC5D4;">$user</span><span style="color: #89DDFF;">->name</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>

Joining Presence
Channels

To join a presence channel, you may use Echo’s join method. The join
method will return a PresenceChannel implementation which, along with exposing the
listen method, allows you to subscribe to the here,
joining, and leaving events.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">chat.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">roomId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">here</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">users</span><span style="color: #D9F5DD;">)</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: #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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">joining</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(user</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">name</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;">.</span><span style="color: #82AAFF;">leaving</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">user</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(user</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">name</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;">.</span><span style="color: #82AAFF;">error</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">error</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">error</span><span style="color: #BFC7D5;">(error);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

The here callback will be executed immediately once the channel is joined
successfully, and will receive an array containing the user information for all of the other
users currently subscribed to the channel. The joining method will be executed when
a new user joins a channel, while the leaving method will be executed when a user
leaves the channel. The error method will be executed when the authentication
endpoint returns an HTTP status code other than 200 or if there is a problem parsing the
returned JSON.

Broadcasting
to Presence Channels

Presence channels may receive events just like public or private channels. Using the example of a
chatroom, we may want to broadcast NewMessage events to the room’s presence
channel. To do so, we’ll return an instance of PresenceChannel from the event’s
broadcastOn method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the channels the event should broadcast on.</span></div><div class="line"><span style="color: #697098;"> *</span></div><div class="line"><span style="color: #697098;"> * </span><span style="color: #C792EA;">@return</span><span style="color: #697098;"> </span><span style="color: #C792EA;">array</span><span style="color: #697098;"><</span><span style="color: #C792EA;">int</span><span style="color: #697098;">, \Illuminate\Broadcasting\Channel></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;">broadcastOn</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">PresenceChannel</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">chat.</span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;">.</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->message->room_id</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>

As with other events, you may use the broadcast helper and the toOthers
method to exclude the current user from receiving the broadcast:

<!-- 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;">NewMessage</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">message</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;">NewMessage</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">message</span><span style="color: #BFC7D5;">))</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toOthers</span><span style="color: #BFC7D5;">();</span></div>

As typical of other types of events, you may listen for events sent to presence channels using
Echo’s listen method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">join</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">chat.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">roomId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">here</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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">joining</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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">leaving</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><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">NewMessage</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

Model Broadcasting

exclamation

Before reading the following documentation about model broadcasting, we
recommend you become familiar with the general concepts of Laravel’s model
broadcasting services as well as how to manually create and listen to broadcast events.

It is common to broadcast events when your application’s Eloquent
models
are created, updated, or deleted. Of course, this can easily be
accomplished by manually defining custom events for Eloquent
model state changes
and marking those events with the
ShouldBroadcast interface.

However, if you are not using these events for any other purposes in your application, it can be
cumbersome to create event classes for the sole purpose of broadcasting them. To remedy this,
Laravel allows you to indicate that an Eloquent model should automatically
broadcast its state changes.

To get started, your Eloquent model should use the
Illuminate\Database\Eloquent\BroadcastsEvents trait. In addition, the
model should define a broadcastOn method, which will return an array
of channels that the model‘s events should broadcast on:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Models;
 
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Database\Eloquent\BroadcastsEvents;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class Post extends Model
{
use BroadcastsEvents, HasFactory;
 
/**
* Get the user that the post belongs to.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
 
/**
* Get the channels that model events should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel|\Illuminate\Database\Eloquent\Model>
*/
public function broadcastOn(string $event): array
{
return [$this, $this->user];
}
}

Once your model includes this trait and defines its broadcast channels, it will
begin automatically broadcasting events when a model instance is created, updated,
deleted, trashed, or restored.

In addition, you may have noticed that the broadcastOn method receives a string
$event argument. This argument contains the type of event that has occurred on the
model and will have a value of created, updated,
deleted, trashed, or restored. By inspecting the value of
this variable, you may determine which channels (if any) the model should broadcast
to for a particular event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the channels that <code>model events should broadcast on.
*
* @return array<string, array>
*/
public function broadcastOn(string $event): array
{
return match ($event) {
'deleted' => [],
default => [$this, $this->user],
};
}

Customizing Model Broadcasting Event
Creation

Occasionally, you may wish to customize how Laravel creates the underlying model
broadcasting event. You may accomplish this by defining a newBroadcastableEvent
method on your Eloquent model. This method should return an
Illuminate\Database\Eloquent\BroadcastableModelEventOccurred instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Database\Eloquent\</span><span style="color: #FFCB8B;">BroadcastableModelEventOccurred</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;"> * Create a new broadcastable <code>model event for the model.
*/
protected function newBroadcastableEvent(string $event): BroadcastableModelEventOccurred
{
return (new BroadcastableModelEventOccurred(
$this, $event
))->dontBroadcastToCurrentUser();
}

Model Broadcasting
Conventions

Channel Conventions

As you may have noticed, the broadcastOn method in the model example
above did not return Channel instances. Instead, Eloquent models were
returned directly. If an Eloquent model instance is returned by your
model‘s broadcastOn method (or is contained in an array returned by
the method), Laravel will automatically instantiate a private channel instance for the
model using the model‘s class name and primary key identifier as the
channel name.

So, an App\Models\User model with an id of 1
would be converted into an Illuminate\Broadcasting\PrivateChannel instance with a
name of App.Models.User.1. Of course, in addition to returning Eloquent
model instances from your model‘s broadcastOn method, you
may return complete Channel instances in order to have full control over the
model‘s channel names:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Broadcasting\</span><span style="color: #FFCB8B;">PrivateChannel</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;"> * Get the channels that <code>model events should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(string $event): array
{
return [
new PrivateChannel('user.'.$this->id)
];
}

If you plan to explicitly return a channel instance from your model‘s
broadcastOn method, you may pass an Eloquent model instance to the
channel’s constructor. When doing so, Laravel will use the model channel
conventions discussed above to convert the Eloquent model into a channel name
string:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Channel</span><span style="color: #BFC7D5;">(</span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->user</span><span style="color: #BFC7D5;">)];</span></div>

If you need to determine the channel name of a model, you may call the
broadcastChannel method on any model instance. For example, this
method returns the string App.Models.User.1 for an App\Models\User
model with an id of 1:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">broadcastChannel</span><span style="color: #BFC7D5;">()</span></div>

Event
Conventions

Since model broadcast events are not associated with an «actual» event within your
application’s App\Events directory, they are assigned a name and a payload based on
conventions. Laravel’s convention is to broadcast the event using the class name of the
model (not including the namespace) and the name of the model event
that triggered the broadcast.

So, for example, an update to the App\Models\Post model would broadcast
an event to your client-side application as PostUpdated with the following payload:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C3E88D;">"<code>model": {
"id": 1,
"title": "My first post"
...
},
...
"socket": "someSocketId",
}

The deletion of the App\Models\User model would broadcast an event
named UserDeleted.

If you would like, you may define a custom broadcast name and payload by adding a
broadcastAs and broadcastWith method to your model. These
methods receive the name of the model event / operation that is occurring, allowing
you to customize the event’s name and payload for each model operation. If
null is returned from the broadcastAs method, Laravel will use the
model broadcasting event name conventions discussed above when broadcasting the
event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * The <code>model event's broadcast name.
*/
public function broadcastAs(string $event): string|null
{
return match ($event) {
'created' => 'post.created',
default => null,
};
}
 
/**
* Get the data to broadcast for the model.
*
* @return array<string, mixed>
*/
public function broadcastWith(string $event): array
{
return match ($event) {
'created' => ['title' => $this->title],
default => ['model' => $this],
};
}

Listening for
Model Broadcasts

Once you have added the BroadcastsEvents trait to your model and
defined your model‘s broadcastOn method, you are ready to start
listening for broadcasted model events within your client-side application. Before
getting started, you may wish to consult the complete documentation on listening for events.

First, use the private method to retrieve an instance of a channel, then call the
listen method to listen for a specified event. Typically, the channel name given to
the private method should correspond to Laravel’s model broadcasting conventions.

Once you have obtained a channel instance, you may use the listen method to listen
for a particular event. Since model broadcast events are not associated with an
«actual» event within your application’s App\Events directory, the event name must be prefixed with a
. to indicate it does not belong to a particular namespace. Each model
broadcast event has a model property which contains all of the broadcastable
properties of the model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">App.Models.User.</span><span style="color: #D3423E;">${</span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">user</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">id</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">.PostUpdated</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(e</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;"><code>model);
});

Client Events

lightbulb

When using Pusher Channels, you must enable the
«Client Events» option in the «App Settings» section of your application dashboard in order to send client
events.

Sometimes you may wish to broadcast an event to other connected clients without hitting your
Laravel application at all. This can be particularly useful for things like «typing»
notifications, where you want to alert users of your application that another user is typing a
message on a given screen.

To broadcast client events, you may use Echo’s whisper method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">chat.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">roomId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">whisper</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">typing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, {</span></div><div class="line"><span style="color: #BFC7D5;">        name: </span><span style="color: #FF5572;">this</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">user</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">name</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

To listen for client events, you may use the listenForWhisper method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">chat.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">roomId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">listenForWhisper</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">typing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">e</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(e</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">name</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

Notifications

By pairing event broadcasting with notifications, your
JavaScript application may receive new notifications as they occur without needing to refresh
the page. Before getting started, be sure to read over the documentation on using the broadcast notification channel.

Once you have configured a notification to use the broadcast channel, you may listen
for the broadcast events using Echo’s notification method. Remember, the channel
name should match the class name of the entity receiving the notifications:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Echo</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">private</span><span style="color: #BFC7D5;">(</span><span style="color: #BFC7D5;">`</span><span style="color: #C3E88D;">App.Models.User.</span><span style="color: #D3423E;">${</span><span style="color: #BFC7D5;">userId</span><span style="color: #D3423E;">}</span><span style="color: #BFC7D5;">`</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">notification</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">(</span><span style="color: #7986E7;">notification</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=></span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;">        console</span><span style="color: #C792EA;">.</span><span style="color: #82AAFF;">log</span><span style="color: #BFC7D5;">(notification</span><span style="color: #C792EA;">.</span><span style="color: #89DDFF;">type</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

In this example, all notifications sent to App\Models\User instances via the
broadcast channel would be received by the callback. A channel authorization
callback for the App.Models.User.{id} channel is included in your application’s
routes/channels.php file.


Cache

Cache

Introduction

Some of the data retrieval or processing tasks performed by your application could be CPU
intensive or take several seconds to complete. When this is the case, it is common to cache the
retrieved data for a time so it can be retrieved quickly on subsequent requests for the same
data. The cached data is usually stored in a very fast data store such as Memcached or Redis.

Thankfully, Laravel provides an expressive, unified API for various cache backends, allowing you
to take advantage of their blazing fast data retrieval and speed up your web application.

Configuration

Your application’s cache configuration file is located at
config/cache.php. In this file, you may specify which cache store you would like to
be used by default throughout your application. Laravel supports popular caching backends like
Memcached, Redis, DynamoDB, and relational databases
out of the box. In addition, a file based cache driver is available, while array
and «null» cache drivers provide convenient cache backends for your automated tests.

The cache configuration file also contains a variety of other options that you may
review. By default, Laravel is configured to use the
database cache driver, which stores the serialized, cached objects in your
application’s database.

Driver Prerequisites

Database

When using the database cache driver, you will need a database table to
contain the cache data. Typically, this is included in Laravel’s default
0001_01_01_000001_create_cache_table.php database migration; however, if your
application does not contain this migration, you may use the
make:cache-table Artisan command to create it:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan make:cache-table
 
php artisan migrate

Memcached

Using the Memcached driver requires the Memcached PECL package to be installed.
You may list all of your Memcached servers in the config/cache.php
configuration file. This file already contains a memcached.servers
entry to get you started:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">memcached</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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">servers</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">host</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;"><code>env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],

If needed, you may set the host option to a UNIX socket path. If you do this, the
port option should be set to 0:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">memcached</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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">servers</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></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">host</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;">/var/run/memcached/memcached.sock</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;">port</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;">0</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">weight</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></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></div>

Redis

Before using a Redis cache with Laravel, you will need to either install the PhpRedis PHP
extension via PECL or install the predis/predis package (~2.0) via Composer. Laravel Sail already includes this extension. In addition, official
Laravel deployment platforms such as Laravel Forge and
Laravel Vapor have the PhpRedis extension installed by
default.

For more information on configuring Redis, consult its Laravel documentation page.

DynamoDB

Before using the DynamoDB cache driver, you must
create a DynamoDB table to store all of the cached data. Typically, this table should be named
cache. However, you should name the table based on the value of the
stores.dynamodb.table configuration value within the
cache configuration file. The table name may also be set via the
DYNAMODB_CACHE_TABLE environment variable.

This table should also have a string partition key with a name that corresponds to the value of
the stores.dynamodb.attributes.key configuration item within your
application’s cache configuration file. By default, the partition key
should be named key.

Typically, DynamoDB will not proactively remove expired items from a table. Therefore, you should
enable Time
to Live (TTL)
on the table. When configuring the table’s TTL settings, you
should set the TTL attribute name to expires_at.

Next, install the AWS SDK so that your Laravel application can communicate with DynamoDB:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require aws/aws-sdk-php

In addition, you should ensure that values are provided for the DynamoDB cache store
configuration options. Typically these options, such as
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, should be defined in your
application’s .env configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">dynamodb</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;">driver</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;">dynamodb</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;">key</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;"><code>env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'table' => env('DYNAMODB_CACHE_TABLE', 'cache'),
'endpoint' => env('DYNAMODB_ENDPOINT'),
],

MongoDB

If you are using MongoDB, a mongodb cache driver is provided by the official
mongodb/laravel-mongodb package and can be configured using a
mongodb database connection. MongoDB supports TTL indexes, which can
be used to automatically clear expired cache items.

For more information on configuring MongoDB, please refer to the MongoDB Cache and
Locks documentation
.

Cache Usage

Obtaining a Cache
Instance

To obtain a cache store instance, you may use the Cache facade, which is what we
will use throughout this documentation. The Cache facade provides convenient, terse
access to the underlying implementations of the Laravel cache contracts:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Controllers;
 
use Illuminate\Support\Facades\Cache;
 
class UserController extends Controller
{
/**
* Show a list of all users of the application.
*/
public function index(): array
{
$value = Cache::get('key');
 
return [
// ...
];
}
}

Accessing
Multiple Cache Stores

Using the Cache facade, you may access various cache stores via the
store method. The key passed to the store method should correspond to
one of the stores listed in the stores configuration array in your
cache configuration file:

<!-- 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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">store</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">file</span><span style="color: #D9F5DD;">'</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;">foo</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">store</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>redis')->put('bar', 'baz', 600); // 10 Minutes

Retrieving Items
From the Cache

The Cache facade’s get method is used to retrieve items from the cache.
If the item does not exist in the cache, null will be returned. If you wish, you
may pass a second argument to the get method specifying the default value you wish
to be returned if the item doesn’t exist:

<!-- 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: #FFCB8B;">Cache</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: #BEC5D4;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cache</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may even pass a closure as the default value. The result of the closure will be returned if
the specified item does not exist in the cache. Passing a closure allows you to defer the
retrieval of default values from a database or other external service:

<!-- 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: #FFCB8B;">Cache</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><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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Determining Item
Existence

The has method may be used to determine if an item exists in the cache. This method
will also return false if the item exists but its value is null:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">has</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"><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>

Incrementing /
Decrementing Values

The increment and decrement methods may be used to adjust the value of
integer items in the cache. Both of these methods accept an optional second argument indicating
the amount by which to increment or decrement the item’s value:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Initialize the value if it does not exist...</span></div><div class="line"><span style="color: #FFCB8B;">Cache</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;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">0</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;">addHours</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Increment or decrement the value...</span></div><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">increment</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"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">increment</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;">$amount</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">decrement</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"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">decrement</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;">$amount</span><span style="color: #BFC7D5;">);</span></div>

Retrieve and Store

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the
requested item doesn’t exist. For example, you may wish to retrieve all users from the cache or,
if they don’t exist, retrieve them from the database and add them to the cache. You
may do this using the Cache::remember method:

<!-- 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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">remember</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$seconds</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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

If the item does not exist in the cache, the closure passed to the remember method
will be executed and its result will be placed in the cache.

You may use the rememberForever method to retrieve an item from the cache or store
it forever if it does not exist:

<!-- 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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">rememberForever</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Stale While Revalidate

When using the Cache::remember method, some users may experience slow response times
if the cached value has expired. For certain types of data, it can be useful to allow partially
stale data to be served while the cached value is recalculated in the background, preventing
some users from experiencing slow response times while cached values are calculated. This is
often referred to as the «stale-while-revalidate» pattern, and the Cache::flexible
method provides an implementation of this pattern.

The flexible method accepts an array that specifies how long the cached value is considered
“fresh” and when it becomes “stale.” The first value in the array represents the number of
seconds the cache is considered fresh, while the second value defines how long it can be served
as stale data before recalculation is necessary.

If a request is made within the fresh period (before the first value), the cache is returned
immediately without recalculation. If a request is made during the stale period (between the two
values), the stale value is served to the user, and a deferred function is registered to refresh the
cached value after the response is sent to the user. If a request is made after the second
value, the cache is considered expired, and the value is recalculated immediately, which may
result in a slower response for the user:

<!-- 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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">flexible</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">10</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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Retrieve and Delete

If you need to retrieve an item from the cache and then delete the item, you may use the
pull method. Like the get method, null will be returned
if the item does not exist in the cache:

<!-- 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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pull</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: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pull</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: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Storing Items in the
Cache

You may use the put method on the Cache facade to store items in the
cache:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><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;">10</span><span style="color: #BFC7D5;">);</span></div>

If the storage time is not passed to the put method, the item will be stored
indefinitely:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Instead of passing the number of seconds as an integer, you may also pass a DateTime
instance representing the desired expiration time of the cached item:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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: #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;">addMinutes</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">));</span></div>

Store if Not Present

The add method will only add the item to the cache if it does not already exist in
the cache store. The method will return true if the item is actually added to the
cache. Otherwise, the method will return false. The add method is an
atomic operation:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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;">key</span><span style="color: #D9F5DD;">'</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: #BEC5D4;">$seconds</span><span style="color: #BFC7D5;">);</span></div>

Storing Items Forever

The forever method may be used to store an item in the cache permanently. Since
these items will not expire, they must be manually removed from the cache using the
forget method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">forever</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: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
lightbulb

If you are using the Memcached driver, items that are stored «forever» may be removed when
the cache reaches its size limit.

Removing Items From
the Cache

You may remove items from the cache using the forget method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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;">key</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

You may also remove items by providing a zero or negative number of expiration seconds:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</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: #D9F5DD;">'</span><span style="color: #C3E88D;">value</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: #FFCB8B;">Cache</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: #D9F5DD;">'</span><span style="color: #C3E88D;">value</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div>

You may clear the entire cache using the flush method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">flush</span><span style="color: #BFC7D5;">();</span></div>
exclamation

Flushing the cache does not respect your configured cache «prefix» and will
remove all entries from the cache. Consider this carefully when clearing a cache which is
shared by other applications.

The Cache Helper

In addition to using the Cache facade, you may also use the global
cache function to retrieve and store data via the cache. When the
cache function is called with a single, string argument, it will return the value
of the given key:

<!-- 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>

If you provide an array of key / value pairs and an expiration time to the function, it will
store values in the cache for the specified duration:

<!-- 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: #BFC7D5;">$</span><span style="color: #BEC5D4;">seconds</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;">addMinutes</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">));</span></div>

When the cache function is called without any arguments, it returns an instance of
the Illuminate\Contracts\Cache\Factory implementation, allowing you to call other
caching methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">cache</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">remember</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$seconds</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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
lightbulb

When testing call to the global cache function, you may use the
Cache::shouldReceive method just as if you were testing the facade.

Atomic Locks

exclamation

To utilize this feature, your application must be using the memcached,
redis, dynamodb, database, file, or
array cache driver as your application’s default cache driver. In addition, all
servers must be communicating with the same central cache server.

Managing Locks

Atomic locks allow for the manipulation of distributed locks without worrying about race
conditions. For example, Laravel Forge uses atomic
locks to ensure that only one remote task is being executed on a server at a time. You may
create and manage locks using the Cache::lock method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Cache</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lock</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</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: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Lock acquired for 10 seconds...</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">release</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

The get method also accepts a closure. After the closure is executed, Laravel will
automatically release the lock:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</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: #F78C6C;">10</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: #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;"> Lock acquired for 10 seconds and automatically released...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

If the lock is not available at the moment you request it, you may instruct Laravel to wait for a
specified number of seconds. If the lock cannot be acquired within the specified time limit, an
Illuminate\Contracts\Cache\LockTimeoutException will be thrown:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Contracts\Cache\</span><span style="color: #FFCB8B;">LockTimeoutException</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lock</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</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: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">try</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">block</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: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> Lock acquired after waiting a maximum of 5 seconds...</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">catch</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB6B;">LockTimeoutException</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$e</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;"> Unable to acquire lock...</span></div><div class="line"><span style="color: #BFC7D5;">} </span><span style="color: #C792EA;">finally</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">release</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

The example above may be simplified by passing a closure to the block method. When a
closure is passed to this method, Laravel will attempt to acquire the lock for the specified
number of seconds and will automatically release the lock once the closure has been executed:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</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: #F78C6C;">10</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">block</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</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;"> Lock acquired after waiting a maximum of 5 seconds...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Managing Locks
Across Processes

Sometimes, you may wish to acquire a lock in one process and release it in another process. For
example, you may acquire a lock during a web request and wish to release the lock at the end of
a queued job that is triggered by that request. In this scenario, you should pass the lock’s
scoped «owner token» to the queued job so that the job can re-instantiate the lock using the
given token.

In the example below, we will dispatch a queued job if a lock is successfully acquired. In
addition, we will pass the lock’s owner token to the queued job via the lock’s
owner method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$podcast</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Podcast</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lock</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">processing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">120</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">ProcessPodcast</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">dispatch</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$podcast</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$lock</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">owner</span><span style="color: #BFC7D5;">());</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Within our application’s ProcessPodcast job, we can restore and release the lock
using the owner token:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">restoreLock</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">processing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">->owner</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">release</span><span style="color: #BFC7D5;">();</span></div>

If you would like to release a lock without respecting its current owner, you may use the
forceRelease method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">lock</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">processing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">forceRelease</span><span style="color: #BFC7D5;">();</span></div>

Adding Custom Cache
Drivers

Writing the Driver

To create our custom cache driver, we first need to implement the
Illuminate\Contracts\Cache\Store contract. So, a
MongoDB cache implementation might look something like this:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Extensions;
 
use Illuminate\Contracts\Cache\Store;
 
class MongoStore implements Store
{
public function get($key) {}
public function many(array $keys) {}
public function put($key, $value, $seconds) {}
public function putMany(array $values, $seconds) {}
public function increment($key, $value = 1) {}
public function decrement($key, $value = 1) {}
public function forever($key, $value) {}
public function forget($key) {}
public function flush() {}
public function getPrefix() {}
}

We just need to implement each of these methods using a MongoDB connection. For an example of how
to implement each of these methods, take a look at the
Illuminate\Cache\MemcachedStore in the Laravel framework source code. Once our
implementation is complete, we can finish our custom driver registration by calling the
Cache facade’s extend method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">extend</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">mongo</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;">Application</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$app</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;">Cache</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">repository</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">MongoStore</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>
lightbulb

If you’re wondering where to put your custom cache driver code, you could create an
Extensions namespace within your app directory. However, keep in
mind that Laravel does not have a rigid application structure and you are free to organize
your application according to your preferences.

Registering the Driver

To register the custom cache driver with Laravel, we will use the extend method on
the Cache facade. Since other service providers may attempt to read cached values
within their boot method, we will register our custom driver within a
booting callback. By using the booting callback, we can ensure that
the custom driver is registered just before the boot method is called on our
application’s service providers but after the register method is called on all of
the service providers. We will register our booting callback within the
register method of our application’s App\Providers\AppServiceProvider
class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Providers;
 
use App\Extensions\MongoStore;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->booting(function () {
Cache::extend('mongo', function (Application $app) {
return Cache::repository(new MongoStore);
});
});
}
 
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// ...
}
}

The first argument passed to the extend method is the name of the driver. This will
correspond to your driver option in the config/cache.php
configuration file. The second argument is a closure that should return an
Illuminate\Cache\Repository instance. The closure will be passed an
$app instance, which is an instance of the service
container
.

Once your extension is registered, update the CACHE_STORE environment
variable or default option within your application’s config/cache.php
configuration file to the name of your extension.

Events

To execute code on every cache operation, you may listen for various events dispatched by the cache:

Event Name
Illuminate\Cache\Events\CacheHit
Illuminate\Cache\Events\CacheMissed
Illuminate\Cache\Events\KeyForgotten
Illuminate\Cache\Events\KeyWritten

To increase performance, you may disable cache events by setting the events
configuration option to false for a given cache store in your
application’s config/cache.php configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>database' => [
'driver' => 'database',
// ...
'events' => false,
],


Cashier Paddle

Laravel Cashier (Paddle)

Introduction

exclamation

This documentation is for Cashier Paddle 2.x’s integration with Paddle Billing. If you’re
still using Paddle Classic, you should use Cashier Paddle 1.x.

Laravel Cashier Paddle provides an
expressive, fluent interface to Paddle’s subscription billing
services. It handles almost all of the boilerplate subscription billing code you are dreading.
In addition to basic subscription management, Cashier can handle: swapping subscriptions,
subscription «quantities», subscription pausing, cancelation grace periods, and more.

Before digging into Cashier Paddle, we recommend you also review Paddle’s concept guides and API documentation.

Upgrading Cashier

When upgrading to a new version of Cashier, it’s important that you carefully review
the upgrade
guide
.

Installation

First, install the Cashier package for Paddle using the Composer package manager:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require laravel/cashier-paddle

Next, you should publish the Cashier migration files using the
vendor:publish Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan vendor:publish --tag="cashier-migrations"

Then, you should run your application’s database migrations. The
Cashier migrations will create a new customers table. In addition, new
subscriptions and subscription_items tables will be created to store
all of your customer’s subscriptions. Lastly, a new transactions table will be
created to store all of the Paddle transactions associated with your customers:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan migrate

exclamation

To ensure Cashier properly handles all Paddle events, remember to set up Cashier’s webhook handling.

Paddle Sandbox

During local and staging development, you should register a Paddle Sandbox account. This
account will give you a sandboxed environment to test and develop your applications
without making actual payments. You may use Paddle’s test card
numbers
to simulate various payment scenarios.

When using the Paddle Sandbox environment, you should set the
PADDLE_SANDBOX environment variable to true within your
application’s .env file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">PADDLE_SANDBOX</span><span style="color: #BFC7D5;">=true</span></div>

After you have finished developing your application you may apply
for a Paddle vendor account
. Before your application is placed into production, Paddle
will need to approve your application’s domain.

Configuration

Billable Model

Before using Cashier, you must add the Billable trait to your user
model definition. This trait provides various methods to allow you to perform
common billing tasks, such as creating subscriptions and updating payment method information:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Billable</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;">User</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">Authenticatable</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Billable</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

If you have billable entities that are not users, you may also add the trait to those classes:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Database\Eloquent\</span><span style="color: #FFCB8B;">Model</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Billable</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;">Team</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">Model</span></div><div class="line"><span style="color: #BFC7D5;">{</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Billable</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

API Keys

Next, you should configure your Paddle keys in your application’s .env
file. You can retrieve your Paddle API keys from the Paddle control panel:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">PADDLE_CLIENT_SIDE_TOKEN</span><span style="color: #BFC7D5;">=your-paddle-client-side-token</span></div><div class="line"><span style="color: #C792EA;">PADDLE_API_KEY</span><span style="color: #BFC7D5;">=your-paddle-api-key</span></div><div class="line"><span style="color: #C792EA;">PADDLE_RETAIN_KEY</span><span style="color: #BFC7D5;">=your-paddle-retain-key</span></div><div class="line"><span style="color: #C792EA;">PADDLE_WEBHOOK_SECRET</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">your-paddle-webhook-secret</span><span style="color: #D9F5DD;">"</span></div><div class="line"><span style="color: #C792EA;">PADDLE_SANDBOX</span><span style="color: #BFC7D5;">=true</span></div>

The PADDLE_SANDBOX environment variable should be set to
true when you are using Paddle’s Sandbox
environment
. The PADDLE_SANDBOX variable should be set to
false if you are deploying your application to production and are using Paddle’s
live vendor environment.

The PADDLE_RETAIN_KEY is optional and should only be set if you’re using Paddle with
Retain.

Paddle JS

Paddle relies on its own JavaScript library to initiate the Paddle checkout widget. You can load
the JavaScript library by placing the @paddleJS Blade directive right before your
application layout’s closing </head> tag:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">head</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    ...</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">@paddleJS</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">head</span><span style="color: #89DDFF;">></span></div>

Currency Configuration

You can specify a locale to be used when formatting money values for display on invoices.
Internally, Cashier utilizes PHP’s
NumberFormatter class
to set the currency locale:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_CURRENCY_LOCALE</span><span style="color: #BFC7D5;">=nl_BE</span></div>

exclamation

In order to use locales other than en, ensure the ext-intl PHP
extension is installed and configured on your server.

Overriding Default
Models

You are free to extend the models used internally by Cashier by defining your own
model and extending the corresponding Cashier model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> CashierSubscription;</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">extends</span><span style="color: #BFC7D5;"> </span><span style="color: #A9C77D;">CashierSubscription</span></div><div class="line"><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>

After defining your model, you may instruct Cashier to use your custom
model via the Laravel\Paddle\Cashier class. Typically, you should
inform Cashier about your custom models in the boot method of your
application’s App\Providers\AppServiceProvider class:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\Cashier\</span><span style="color: #FFCB8B;">Subscription</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\Cashier\</span><span style="color: #FFCB8B;">Transaction</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useSubscriptionModel</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Subscription</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">useTransactionModel</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Transaction</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>

Quickstart

Selling Products

lightbulb

Before utilizing Paddle Checkout, you should define Products with fixed prices in your
Paddle dashboard. In addition, you should configure Paddle’s webhook handling.

Offering product and subscription billing via your application can be intimidating. However,
thanks to Cashier and Paddle’s Checkout
Overlay
, you can easily build modern, robust payment integrations.

To charge customers for non-recurring, single-charge products, we’ll utilize Cashier to charge
customers with Paddle’s Checkout Overlay, where they will provide their payment details and
confirm their purchase. Once the payment has been made via the Checkout Overlay, the customer
will be redirected to a success URL of your choosing within your application:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_deluxe_album</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('buy', ['checkout' => $checkout]);
})->name('checkout');

As you can see in the example above, we will utilize Cashier’s provided checkout
method to create a checkout object to present the customer the Paddle Checkout Overlay for a
given «price identifier». When using Paddle, «prices» refer to defined prices for
specific products
.

If necessary, the checkout method will automatically create a customer in Paddle and
connect that Paddle customer record to the corresponding user in your application’s
database. After completing the checkout session, the customer will be redirected to
a dedicated success page where you can display an informational message to the customer.

In the buy view, we will include a button to display the Checkout
Overlay. The paddle-button Blade component is included with Cashier Paddle;
however, you may also manually render an
overlay checkout
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Buy Product</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

Providing Meta Data to Paddle Checkout

When selling products, it’s common to keep track of completed orders and purchased products via
Cart and Order models defined by your own application.
When redirecting customers to Paddle’s Checkout Overlay to complete a purchase, you may need to
provide an existing order identifier so that you can associate the completed purchase with the
corresponding order when the customer is redirected back to your application.

To accomplish this, you may provide an array of custom data to the checkout method.
Let’s imagine that a pending Order is created within our application when a user
begins the checkout process. Remember, the Cart and Order
models in this example are illustrative and not provided by Cashier. You are free
to implement these concepts based on the needs of your own application:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Cart</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;">Order</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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/cart/{cart}/checkout</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: #BFC7D5;">, </span><span style="color: #FFCB8B;">Cart</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$cart</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">create</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">cart_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: #BEC5D4;">$cart</span><span style="color: #89DDFF;">->id</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_ids</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;">$cart</span><span style="color: #89DDFF;">->price_ids</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</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;">incomplete</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: #BFC7D5;"> </span><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">->price_ids</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">customData</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">order_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: #BEC5D4;">$order</span><span style="color: #89DDFF;">->id</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: #82AAFF;"><code>view('billing', ['checkout' => $checkout]);
})->name('checkout');

As you can see in the example above, when a user begins the checkout process, we will provide all
of the cart / order’s associated Paddle price identifiers to the checkout method.
Of course, your application is responsible for associating these items with the «shopping cart»
or order as a customer adds them. We also provide the order’s ID to the Paddle Checkout Overlay
via the customData method.

Of course, you will likely want to mark the order as «complete» once the customer has finished
the checkout process. To accomplish this, you may listen to the webhooks dispatched by Paddle
and raised via events by Cashier to store order information in your database.

To get started, listen for the TransactionCompleted event dispatched by Cashier.
Typically, you should register the event listener in the boot method of your
application’s AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Listeners\</span><span style="color: #FFCB8B;">CompleteOrder</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;">Event</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\Events\</span><span style="color: #FFCB8B;">TransactionCompleted</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: #FFCB6B;">Event</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">listen</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">TransactionCompleted</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">CompleteOrder</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>

In this example, the CompleteOrder listener might look like the following:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">namespace</span><span style="color: #BFC7D5;"> App\Listeners;</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;">Order</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\Events\</span><span style="color: #FFCB8B;">TransactionCompleted</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;">CompleteOrder</span></div><div class="line"><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: #697098;">     * Handle the incoming Cashier webhook event.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</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;">handle</span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">TransactionCompleted</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$event</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: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$event</span><span style="color: #89DDFF;">->payload</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">data</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">][</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">custom_data</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">][</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">order_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: #82AAFF;">null</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Order</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findOrFail</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$orderId</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$order</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">update</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</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;">completed</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>

Please refer to Paddle’s documentation for more information on the data
contained by the transaction.completed event
.

Selling
Subscriptions

lightbulb

Before utilizing Paddle Checkout, you should define Products with fixed prices in your
Paddle dashboard. In addition, you should configure Paddle’s webhook handling.

Offering product and subscription billing via your application can be intimidating. However,
thanks to Cashier and Paddle’s Checkout
Overlay
, you can easily build modern, robust payment integrations.

To learn how to sell subscriptions using Cashier and Paddle’s Checkout Overlay, let’s consider
the simple scenario of a subscription service with a basic monthly
(price_basic_monthly) and yearly (price_basic_yearly) plan. These two
prices could be grouped under a «Basic» product (pro_basic) in our Paddle
dashboard. In addition, our subscription service might offer an Expert plan as
pro_expert.

First, let’s discover how a customer can subscribe to our services. Of course, you can imagine
the customer might click a «subscribe» button for the Basic plan on our application’s pricing
page. This button will invoke a Paddle Checkout Overlay for their chosen plan. To get started,
let’s initiate a checkout session via the checkout method:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('subscribe', ['checkout' => $checkout]);
})->name('subscribe');

In the subscribe view, we will include a button to display the Checkout
Overlay. The paddle-button Blade component is included with Cashier Paddle;
however, you may also manually render an
overlay checkout
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Subscribe</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

Now, when the Subscribe button is clicked, the customer will be able to enter their payment
details and initiate their subscription. To know when their subscription has actually started
(since some payment methods require a few seconds to process), you should also configure Cashier’s webhook handling.

Now that customers can start subscriptions, we need to restrict certain portions of our
application so that only subscribed users can access them. Of course, we can always determine a
user’s current subscription status via the subscribed method provided by Cashier’s
Billable trait:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribed</span><span style="color: #BFC7D5;">())</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

We can even easily determine if a user is subscribed to specific product or price:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToProduct</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pro_basic</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed to our Basic product.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div><div class="line"> </div><div class="line"><span style="color: #C792EA;">@if </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_basic_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">))</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">You are subscribed to our monthly Basic plan.</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">p</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endif</span></div>

Building a Subscribed Middleware

For convenience, you may wish to create a middleware
which determines if the incoming request is from a subscribed user. Once this
middleware has been defined, you may easily assign it to a route to
prevent users that are not subscribed from accessing the route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class Subscribed
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
if (! $request->user()?->subscribed()) {
// Redirect user to billing page and ask them to subscribe...
return redirect('/subscribe');
}
 
return $next($request);
}
}

Once the middleware has been defined, you may assign it to a route:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Http\Middleware\</span><span style="color: #FFCB8B;">Subscribed</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/dashboard</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>middleware([Subscribed::class]);

Allowing Customers to
Manage Their Billing Plan

Of course, customers may want to change their subscription plan to another product or «tier». In
our example from above, we’d want to allow the customer to change their plan from a monthly
subscription to a yearly subscription. For this you’ll need to implement something like a button
that leads to the below route:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription/{price}/swap</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: #BFC7D5;">, </span><span style="color: #BEC5D4;">$price</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;">); </span><span style="color: #697098;">//</span><span style="color: #697098;"> With "$price" being "price_basic_yearly" for this example.</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>route('dashboard');
})->name('subscription.swap');

Besides swapping plans you’ll also need to allow your customers to cancel their subscription.
Like swapping plans, provide a button that leads to the following route:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/subscription/cancel</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: #BFC7D5;">, </span><span style="color: #BEC5D4;">$price</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancel</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: #82AAFF;">redirect</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>route('dashboard');
})->name('subscription.cancel');

And now your subscription will get canceled at the end of its billing period.

lightbulb

As long as you have configured Cashier’s webhook handling, Cashier will
automatically keep your application’s Cashier-related database tables in sync
by inspecting the incoming webhooks from Paddle. So, for example, when you cancel a
customer’s subscription via Paddle’s dashboard, Cashier will receive the corresponding
webhook and mark the subscription as «canceled» in your application’s database.

Checkout Sessions

Most operations to bill customers are performed using «checkouts» via Paddle’s Checkout Overlay
widget
or by utilizing inline
checkout
.

Before processing checkout payments using Paddle, you should define your application’s default
payment link
in your Paddle checkout settings dashboard.

Overlay Checkout

Before displaying the Checkout Overlay widget, you must generate a checkout session using
Cashier. A checkout session will inform the checkout widget of the billing operation that should
be performed:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_34567</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('billing', ['checkout' => $checkout]);
});

Cashier includes a paddle-button Blade
component
. You may pass the checkout session to this component as a «prop». Then, when
this button is clicked, Paddle’s checkout widget will be displayed:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Subscribe</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

By default, this will display the widget using Paddle’s default styling. You can customize the
widget by adding Paddle
supported attributes
like the data-theme='light' attribute to the
component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:url</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$payLink</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">data-theme</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">light</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Subscribe</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

The Paddle checkout widget is asynchronous. Once the user creates a subscription within the
widget, Paddle will send your application a webhook so that you may properly update the
subscription state in your application’s database. Therefore, it’s important that
you properly set up webhooks to accommodate for state
changes from Paddle.

exclamation

After a subscription state change, the delay for receiving the corresponding webhook is
typically minimal but you should account for this in your application by considering that
your user’s subscription might not be immediately available after completing the checkout.

Manually Rendering an Overlay Checkout

You may also manually render an overlay checkout without using Laravel’s built-in Blade
components. To get started, generate the checkout session as
demonstrated in previous examples
:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_34567</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('billing', ['checkout' => $checkout]);
});

Next, you may use Paddle.js to initialize the checkout. In this example, we will create a link
that is assigned the paddle_button class. Paddle.js will detect this class and
display the overlay checkout when the link is clicked:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
$items = $checkout->getItems();
$customer = $checkout->getCustomer();
$custom = $checkout->getCustomData();
?>
 
<a
href='#!'
class='paddle_button'
data-items='{!! json_encode($items) !!}'
@if ($customer) data-customer-id='{{ $customer->paddle_id }}' @endif
@if ($custom) data-custom-data='{{ json_encode($custom) }}' @endif
@if ($returnUrl = $checkout->getReturnUrl()) data-success-url='{{ $returnUrl }}' @endif
>
Buy Product
</spana>

Inline Checkout

If you don’t want to make use of Paddle’s «overlay» style checkout widget, Paddle also provides
the option to display the widget inline. While this approach does not allow you to adjust any of
the checkout’s HTML fields, it allows you to embed the widget within your application.

To make it easy for you to get started with inline checkout, Cashier includes a
paddle-checkout Blade component. To get started, you should generate a checkout session:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_34567</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('billing', ['checkout' => $checkout]);
});

Then, you may pass the checkout session to the component’s checkout attribute:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-checkout</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">w-full</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> /></span></div>

To adjust the height of the inline checkout component, you may pass the height
attribute to the Blade component:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-checkout</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">w-full</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">height</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">500</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> /></span></div>

Please consult Paddle’s guide on
Inline Checkout
and available
checkout settings
for further details on the inline checkout’s customization options.

Manually Rendering an Inline Checkout

You may also manually render an inline checkout without using Laravel’s built-in Blade
components. To get started, generate the checkout session as
demonstrated in previous examples
:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_34567</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('dashboard'));
 
return view('billing', ['checkout' => $checkout]);
});

Next, you may use Paddle.js to initialize the checkout. In this example, we will demonstrate this
using Alpine.js; however, you are free to
modify this example for your own frontend stack:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
$options = $checkout->options();
 
$options['settings']['frameTarget'] = 'paddle-checkout';
$options['settings']['frameInitialHeight'] = 366;
?>
 
<div class="paddle-checkout" x-data="{}" x-init="
Paddle.Checkout.open(@json($options));
">
</spandiv>

Guest Checkouts

Sometimes, you may need to create a checkout session for users that do not need an account with
your application. To do so, you may use the guest method:

<!-- Syntax highlighted by torchlight.dev --><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;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Checkout</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Checkout</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">guest</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_34567</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('home'));
 
return view('billing', ['checkout' => $checkout]);
});

Then, you may provide the checkout session to the Paddle button
or inline checkout Blade components.

Price Previews

Paddle allows you to customize prices per currency, essentially allowing you to
configure different prices for different countries. Cashier Paddle allows you to
retrieve all of these prices using the previewPrices method. This method accepts
the price IDs you wish to retrieve prices for:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pre<code>viewPrices(['pri_123', 'pri_456']);

The currency will be determined based on the IP address of the request; however, you may
optionally provide a specific country to retrieve prices for:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pre<code>viewPrices(['pri_123', 'pri_456'], ['address' => [
'country_code' => 'BE',
'postal_code' => '1234',
]]);

After retrieving the prices you may display them however you wish:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">->product</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: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">total</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div>

You may also display the subtotal price and tax amount separately:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">->product</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: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subtotal</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;"> (+ </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tax</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;"> tax)</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div>

For more information, checkout
Paddle’s API documentation regarding price previews
.

Customer Price
Previews

If a user is already a customer and you would like to display the prices that apply to that
customer, you may do so by retrieving the prices directly from the customer instance:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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: #89DDFF;">-></span><span style="color: #82AAFF;">pre<code>viewPrices(['pri_123', 'pri_456']);

Internally, Cashier will use the user’s customer ID to retrieve the prices in their currency. So,
for example, a user living in the United States will see prices in US dollars while a user in
Belgium will see prices in Euros. If no matching currency can be found, the default currency of
the product will be used. You can customize all prices of a product or subscription plan in the
Paddle control panel.

Discounts

You may also choose to display prices after a discount. When calling the
previewPrices method, you provide the discount ID via the discount_id
option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pre<code>viewPrices(['pri_123', 'pri_456'], [
'discount_id' => 'dsc_123'
]);

Then, display the calculated prices:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$prices</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">->product</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: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$price</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">total</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">li</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">ul</span><span style="color: #89DDFF;">></span></div>

Customers

Customer Defaults

Cashier allows you to define some useful defaults for your customers when creating checkout
sessions. Setting these defaults allow you to pre-fill a customer’s email address and name so
that they can immediately move on to the payment portion of the checkout widget. You can set
these defaults by overriding the following methods on your billable model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Get the customer's name to associate with Paddle.</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;">paddleName</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;">|</span><span style="color: #C792EA;">null</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: #FF5572;">$this</span><span style="color: #89DDFF;">->name</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;"> * Get the customer's email address to associate with Paddle.</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;">paddleEmail</span><span style="color: #D9F5DD;">()</span><span style="color: #89DDFF;">:</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;">|</span><span style="color: #C792EA;">null</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: #FF5572;">$this</span><span style="color: #89DDFF;">->email</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

These defaults will be used for every action in Cashier that generates a checkout session.

Retrieving Customers

You can retrieve a customer by their Paddle Customer ID using the
Cashier::findBillable method. This method will return an instance of the billable
model:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">findBillable</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$customerId</span><span style="color: #BFC7D5;">);</span></div>

Creating Customers

Occasionally, you may wish to create a Paddle customer without beginning a subscription. You may
accomplish this using the createAsCustomer method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$customer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAsCustomer</span><span style="color: #BFC7D5;">();</span></div>

An instance of Laravel\Paddle\Customer is returned. Once the customer has been
created in Paddle, you may begin a subscription at a later date. You may provide an optional
$options array to pass in any additional customer
creation parameters that are supported by the Paddle API
:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$customer</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAsCustomer</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$options</span><span style="color: #BFC7D5;">);</span></div>

Subscriptions

Creating Subscriptions

To create a subscription, first retrieve an instance of your billable model from
your database, which will typically be an instance of App\Models\User.
Once you have retrieved the model instance, you may use the subscribe
method to create the model‘s checkout session:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/user/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">12345</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('home'));
 
return view('billing', ['checkout' => $checkout]);
});

The first argument given to the subscribe method is the specific price the user is
subscribing to. This value should correspond to the price’s identifier in Paddle. The
returnTo method accepts a URL that your user will be redirected to after they
successfully complete the checkout. The second argument passed to the subscribe
method should be the internal «type» of the subscription. If your application only offers a
single subscription, you might call this default or primary. This
subscription type is only for internal application usage and is not meant to be displayed to
users. In addition, it should not contain spaces and it should never be changed after creating
the subscription.

You may also provide an array of custom metadata regarding the subscription using the
customData method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">12345</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">customData</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: #89DDFF;">=></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"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('home'));

Once a subscription checkout session has been created, the checkout session may be provided to
the paddle-button Blade component that is included
with Cashier Paddle:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Subscribe</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

After the user has finished their checkout, a subscription_created webhook will be
dispatched from Paddle. Cashier will receive this webhook and setup the subscription for your
customer. In order to make sure all webhooks are properly received and handled by your
application, ensure you have properly setup webhook
handling
.

Checking Subscription
Status

Once a user is subscribed to your application, you may check their subscription status using a
variety of convenient methods. First, the subscribed method returns
true if the user has a valid subscription, even if the subscription is currently
within its trial period:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribed</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>

If your application offers multiple subscriptions, you may specify the subscription when invoking
the subscribed method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribed</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

The subscribed method also makes a great candidate for a route middleware, allowing you to
filter access to routes and controllers based on the user’s
subscription status:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class EnsureUserIsSubscribed
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($request->user() && ! $request->user()->subscribed()) {
// This user is not a paying customer...
return redirect('/billing');
}
 
return $next($request);
}
}

If you would like to determine if a user is still within their trial period, you may use the
onTrial method. This method can be useful for determining if you should display a
warning to the user that they are still on their trial period:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</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>

The subscribedToPrice method may be used to determine if the user is subscribed to a
given plan based on a given Paddle price ID. In this example, we will determine if the user’s
default subscription is actively subscribed to the monthly price:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribedToPrice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$monthly</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;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

The recurring method may be used to determine if the user is currently on an active
subscription and is no longer within their trial period or on a grace period:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">recurring</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>

Canceled Subscription
Status

To determine if the user was once an active subscriber but has canceled their subscription, you
may use the canceled method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</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>

You may also determine if a user has canceled their subscription, but are still on their «grace
period» until the subscription fully expires. For example, if a user cancels a subscription on
March 5th that was originally scheduled to expire on March 10th, the user is on their «grace
period» until March 10th. In addition, the subscribed method will still return
true during this time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</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>

Past Due Status

If a payment fails for a subscription, it will be marked as past_due. When your
subscription is in this state it will not be active until the customer has updated their payment
information. You may determine if a subscription is past due using the pastDue
method on the subscription instance:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pastDue</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>

When a subscription is past due, you should instruct the user to update their payment information.

If you would like subscriptions to still be considered valid when they are past_due,
you may use the keepPastDueSubscriptionsActive method provided by Cashier.
Typically, this method should be called in the register method of your
AppServiceProvider:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Cashier</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;"> * Register 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;">register</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;">Cashier</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">keepPastDueSubscriptionsActive</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>
exclamation

When a subscription is in a past_due state it cannot be changed until payment
information has been updated. Therefore, the swap and
updateQuantity methods will throw an exception when the subscription is in a
past_due state.

Subscription Scopes

Most subscription states are also available as query scopes so that you may easily query your
database for subscriptions that are in a given state:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Get all valid subscriptions...</span></div><div class="line"><span style="color: #BEC5D4;">$subscriptions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">valid</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Get all of the canceled subscriptions for a user...</span></div><div class="line"><span style="color: #BEC5D4;">$subscriptions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscriptions</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div>

A complete list of available scopes is available below:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">valid</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">expiredTrial</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notOnTrial</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">active</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">recurring</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pastDue</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">paused</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notPaused</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onPausedGracePeriod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notOnPausedGracePeriod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">canceled</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notCanceled</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #FFCB8B;">Subscription</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">query</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">notOnGracePeriod</span><span style="color: #BFC7D5;">();</span></div>

Subscription Single
Charges

Subscription single charges allow you to charge subscribers with a one-time charge on top of
their subscriptions. You must provide one or multiple price ID’s when invoking the
charge method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Charge a single price...</span></div><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_123</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;"> Charge multiple prices at once...</span></div><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">charge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div>

The charge method will not actually charge the customer until the next billing
interval of their subscription. If you would like to bill the customer immediately, you may use
the chargeAndInvoice method instead:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chargeAndInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Updating Payment
Information

Paddle always saves a payment method per subscription. If you want to update the default payment
method for a subscription, you should redirect your customer to Paddle’s hosted payment method
update page using the redirectToUpdatePaymentMethod method on the subscription
model:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/update-payment-method</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: #BEC5D4;">$user</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">user</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;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">redirectToUpdatePaymentMethod</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

When a user has finished updating their information, a subscription_updated webhook
will be dispatched by Paddle and the subscription details will be updated in your application’s
database.

Changing Plans

After a user has subscribed to your application, they may occasionally want to change to a new
subscription plan. To update the subscription plan for a user, you should pass the Paddle
price’s identifier to the subscription’s swap method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

If you would like to swap plans and immediately invoice the user instead of waiting for their
next billing cycle, you may use the swapAndInvoice method:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swapAndInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Prorations

By default, Paddle prorates charges when swapping between plans. The noProrate
method may be used to update the subscriptions without prorating the charges:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

If you would like to disable proration and invoice customers immediately, you may use the
swapAndInvoice method in combination with noProrate:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swapAndInvoice</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Or, to not bill your customer for a subscription change, you may utilize the
doNotBill method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doNotBill</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$premium</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

For more information on Paddle’s proration policies, please consult Paddle’s proration
documentation
.

Subscription Quantity

Sometimes subscriptions are affected by «quantity». For example, a project management application
might charge $10 per month per project. To easily increment or decrement your subscription’s
quantity, use the incrementQuantity and decrementQuantity methods:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Add five to the subscription's current quantity...</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</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;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">decrementQuantity</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Subtract five from the subscription's current quantity...</span></div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">decrementQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">);</span></div>

Alternatively, you may set a specific quantity using the updateQuantity method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div>

The noProrate method may be used to update the subscription’s quantity without
prorating the charges:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">noProrate</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">updateQuantity</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">);</span></div>

Quantities for Subscriptions With
Multiple Products

If your subscription is a subscription with
multiple products
, you should pass the ID of the price whose quantity you wish to
increment or decrement as the second argument to the increment / decrement methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">incrementQuantity</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;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Subscriptions With Multiple Products

Subscription
with multiple products
allow you to assign multiple billing products to a single
subscription. For example, imagine you are building a customer service «helpdesk» application
that has a base subscription price of $10 per month but offers a live chat add-on product for an
additional $15 per month.

When creating subscription checkout sessions, you may specify multiple products for a given
subscription by passing an array of prices as the first argument to the subscribe
method:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/user/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">subscribe</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_monthly</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_chat</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;"><code>view('billing', ['checkout' => $checkout]);
});

In the example above, the customer will have two prices attached to their default
subscription. Both prices will be charged on their respective billing intervals. If necessary,
you may pass an associative array of key / value pairs to indicate a specific quantity for each
price:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</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;">5</span><span style="color: #BFC7D5;">]);</span></div>

If you would like to add another price to an existing subscription, you must use the
subscription’s swap method. When invoking the swap method, you should
also include the subscription’s current prices and quantities as well:

<!-- 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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_original</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>

The example above will add the new price, but the customer will not be billed for it until their
next billing cycle. If you would like to bill the customer immediately you may use the
swapAndInvoice method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swapAndInvoice</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_chat</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_original</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>

You may remove prices from subscriptions using the swap method and omitting the
price you want to remove:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price_original</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>
exclamation

You may not remove the last price on a subscription. Instead, you should simply cancel the
subscription.

Multiple Subscriptions

Paddle allows your customers to have multiple subscriptions simultaneously. For example, you may
run a gym that offers a swimming subscription and a weight-lifting subscription, and each
subscription may have different pricing. Of course, customers should be able to subscribe to
either or both plans.

When your application creates subscriptions, you may provide the type of the subscription to the
subscribe method as the second argument. The type may be any string that represents
the type of subscription the user is initiating:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">/swimming/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$swimmingMonthly</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;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</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: #82AAFF;"><code>view('billing', ['checkout' => $checkout]);
});

In this example, we initiated a monthly swimming subscription for the customer. However, they may
want to swap to a yearly subscription at a later time. When adjusting the customer’s
subscription, we can simply swap the price on the swimming subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">swap</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$swimmingYearly</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;">pri_456</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Of course, you may also cancel the subscription entirely:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">swimming</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancel</span><span style="color: #BFC7D5;">();</span></div>

Pausing Subscriptions

To pause a subscription, call the pause method on the user’s subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pause</span><span style="color: #BFC7D5;">();</span></div>

When a subscription is paused, Cashier will automatically set the paused_at column
in your database. This column is used to determine when the paused
method should begin returning true. For example, if a customer pauses a
subscription on March 1st, but the subscription was not scheduled to recur until March 5th, the
paused method will continue to return false until March 5th. This is
because a user is typically allowed to continue using an application until the end of their
billing cycle.

By default, pausing happens at the next billing interval so the customer can use the remainder of
the period they paid for. If you want to pause a subscription immediately, you may use the
pauseNow method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pauseNow</span><span style="color: #BFC7D5;">();</span></div>

Using the pauseUntil method, you can pause the subscription until a specific moment
in time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pauseUntil</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;">addMonth</span><span style="color: #BFC7D5;">());</span></div>

Or, you may use the pauseNowUntil method to immediately pause the subscription until
a given point in time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pauseNowUntil</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;">addMonth</span><span style="color: #BFC7D5;">());</span></div>

You may determine if a user has paused their subscription but are still on their «grace period»
using the onPausedGracePeriod method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onPausedGracePeriod</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>

To resume a paused subscription, you may invoke the resume method on the
subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">resume</span><span style="color: #BFC7D5;">();</span></div>
exclamation

A subscription cannot be modified while it is paused. If you want to swap to a different
plan or update quantities you must resume the subscription first.

Canceling Subscriptions

To cancel a subscription, call the cancel method on the user’s subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancel</span><span style="color: #BFC7D5;">();</span></div>

When a subscription is canceled, Cashier will automatically set the ends_at column
in your database. This column is used to determine when the subscribed
method should begin returning false. For example, if a customer cancels a
subscription on March 1st, but the subscription was not scheduled to end until March 5th, the
subscribed method will continue to return true until March 5th. This
is done because a user is typically allowed to continue using an application until the end of
their billing cycle.

You may determine if a user has canceled their subscription but are still on their «grace period»
using the onGracePeriod method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGracePeriod</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>

If you wish to cancel a subscription immediately, you may call the cancelNow method
on the subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cancelNow</span><span style="color: #BFC7D5;">();</span></div>

To stop a subscription on its grace period from canceling, you may invoke the
stopCancelation method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">stopCancelation</span><span style="color: #BFC7D5;">();</span></div>
exclamation

Paddle’s subscriptions cannot be resumed after cancelation. If your customer wishes to
resume their subscription, they will have to create a new subscription.

Subscription Trials

With Payment Method Up
Front

If you would like to offer trial periods to your customers while still collecting payment method
information up front, you should use set a trial time in the Paddle dashboard on the price your
customer is subscribing to. Then, initiate the checkout session as normal:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/user/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('home'));
 
return view('billing', ['checkout' => $checkout]);
});

When your application receives the subscription_created event, Cashier will set the
trial period ending date on the subscription record within your application’s
database as well as instruct Paddle to not begin billing the customer until after
this date.

exclamation

If the customer’s subscription is not canceled before the trial ending date they will be
charged as soon as the trial expires, so you should be sure to notify your users of their
trial ending date.

You may determine if the user is within their trial period using either the onTrial
method of the user instance or the onTrial method of the subscription instance. The
two examples below are equivalent:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</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>

To determine if an existing trial has expired, you may use the hasExpiredTrial
methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasExpiredTrial</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasExpiredTrial</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>

To determine if a user is on trial for a specific subscription type, you may provide the type to
the onTrial or hasExpiredTrial methods:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasExpiredTrial</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</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>

Without Payment
Method Up Front

If you would like to offer trial periods without collecting the user’s payment method information
up front, you may set the trial_ends_at column on the customer record attached to
your user to your desired trial ending date. This is typically done during user registration:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">create</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><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">createAsCustomer</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">trial_ends_at</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;">now</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">addDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

Cashier refers to this type of trial as a «generic trial», since it is not attached to any
existing subscription. The onTrial method on the User instance will
return true if the current date is not past the value of
trial_ends_at:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> User is within their trial period...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Once you are ready to create an actual subscription for the user, you may use the
subscribe method as usual:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/user/subscribe</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscribe</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_monthly</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">returnTo</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;"><code>route('home'));
 
return view('billing', ['checkout' => $checkout]);
});

To retrieve the user’s trial ending date, you may use the trialEndsAt method. This
method will return a Carbon date instance if a user is on a trial or null if they
aren’t. You may also pass an optional subscription type parameter if you would like to get the
trial ending date for a specific subscription other than the default one:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onTrial</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">default</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$trialEndsAt</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">trialEndsAt</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

You may use the onGenericTrial method if you wish to know specifically that the user
is within their «generic» trial period and has not created an actual subscription yet:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">onGenericTrial</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #697098;">//</span><span style="color: #697098;"> User is within their "generic" trial period...</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Extend or Activate a
Trial

You can extend an existing trial period on a subscription by invoking the
extendTrial method and specifying the moment in time that the trial should end:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">extendTrial</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;">addDays</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">));</span></div>

Or, you may immediately activate a subscription by ending its trial by calling the
activate method on the subscription:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">activate</span><span style="color: #BFC7D5;">();</span></div>

Handling Paddle Webhooks

Paddle can notify your application of a variety of events via webhooks. By default, a
route that points to Cashier’s webhook controller is registered by the
Cashier service provider. This controller will handle all incoming webhook
requests.

By default, this controller will automatically handle canceling subscriptions that
have too many failed charges, subscription updates, and payment method changes; however, as
we’ll soon discover, you can extend this controller to handle any Paddle webhook
event you like.

To ensure your application can handle Paddle webhooks, be sure to configure the webhook URL in
the Paddle control panel
. By default, Cashier’s webhook controller responds
to the /paddle/webhook URL path. The full list of all webhooks you should enable in
the Paddle control panel are:

exclamation

Make sure you protect incoming requests with Cashier’s included webhook signature
verification
middleware.

Webhooks and CSRF
Protection

Since Paddle webhooks need to bypass Laravel’s CSRF protection, you
should ensure that Laravel does not attempt to verify the CSRF token for incoming Paddle
webhooks. To accomplish this, you should exclude paddle/* from CSRF protection in
your application’s bootstrap/app.php file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">withMiddleware</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;">Middleware</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$<code>middleware) {
$middleware->validateCsrfTokens(except: [
'paddle/*',
]);
})

Webhooks and Local
Development

For Paddle to be able to send your application webhooks during local development, you will need
to expose your application via a site sharing service such as Ngrok or Expose. If you are developing your
application locally using Laravel Sail, you may use Sail’s site sharing command.

Defining Webhook
Event Handlers

Cashier automatically handles subscription cancelation on failed charges and other common Paddle
webhooks. However, if you have additional webhook events you would like to handle, you may do so
by listening to the following events that are dispatched by Cashier:

Both events contain the full payload of the Paddle webhook. For example, if you wish to handle
the transaction.billed webhook, you may register a listener that will handle the event:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace App\Listeners;
 
use Laravel\Paddle\Events\WebhookReceived;
 
class PaddleEventListener
{
/**
* Handle received Paddle webhooks.
*/
public function handle(WebhookReceived $event): void
{
if ($event->payload['event_type'] === 'transaction.billed') {
// Handle the incoming event...
}
}
}

Cashier also emit events dedicated to the type of the received webhook. In addition to the full
payload from Paddle, they also contain the relevant models that were used to
process the webhook such as the billable model, the subscription, or the receipt:

  • Laravel\Paddle\Events\CustomerUpdated
  • Laravel\Paddle\Events\TransactionCompleted
  • Laravel\Paddle\Events\TransactionUpdated
  • Laravel\Paddle\Events\SubscriptionCreated
  • Laravel\Paddle\Events\SubscriptionUpdated
  • Laravel\Paddle\Events\SubscriptionPaused
  • Laravel\Paddle\Events\SubscriptionCanceled

You can also override the default, built-in webhook route by defining the
CASHIER_WEBHOOK environment variable in your application’s
.env file. This value should be the full URL to your webhook route and
needs to match the URL set in your Paddle control panel:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">CASHIER_WEBHOOK</span><span style="color: #BFC7D5;">=https://example.com/my-paddle-webhook-url</span></div>

Verifying Webhook
Signatures

To secure your webhooks, you may use Paddle’s webhook
signatures
. For convenience, Cashier automatically includes a middleware
which validates that the incoming Paddle webhook request is valid.

To enable webhook verification, ensure that the PADDLE_WEBHOOK_SECRET
environment variable is defined in your application’s .env file. The
webhook secret may be retrieved from your Paddle account dashboard.

Single Charges

Charging for Products

If you would like to initiate a product purchase for a customer, you may use the
checkout method on a billable model instance to generate a checkout
session for the purchase. The checkout method accepts one or multiple price ID’s.
If necessary, an associative array may be used to provide the quantity of the product that is
being purchased:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">Route</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;">/buy</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: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$request</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;">checkout</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_socks</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;">5</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: #82AAFF;"><code>view('buy', ['checkout' => $checkout]);
});

After generating the checkout session, you may use Cashier’s provided paddle-button
Blade component to allow the user to view the
Paddle checkout widget and complete the purchase:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">:checkout</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">$checkout</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">px-8 py-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    Buy</span></div><div class="line"><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">x-paddle-button</span><span style="color: #89DDFF;">></span></div>

A checkout session has a customData method, allowing you to pass any custom data you
wish to the underlying transaction creation. Please consult the Paddle
documentation
to learn more about the options available to you when passing custom data:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$checkout</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">checkout</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_tshirt</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">customData</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">custom_option</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;">$value</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;">    ]);</span></div>

Refunding Transactions

Refunding transactions will return the refunded amount to your customer’s payment method that was
used at the time of purchase. If you need to refund a Paddle purchase, you may use the
refund method on a Cashier\Paddle\Transaction model. This
method accepts a reason as the first argument, one or more price ID’s to refund with optional
amounts as an associative array. You may retrieve the transactions for a given billable
model using the transactions method.

For example, imagine we want to refund a specific transaction for prices pri_123 and
pri_456. We want to fully refund pri_123, but only refund two dollars
for pri_456:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$transaction</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">transactions</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">refund</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Accidental charge</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;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #697098;">//</span><span style="color: #697098;"> Fully refund this price...</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_456</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;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #697098;">//</span><span style="color: #697098;"> Only partially refund this price...</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

The example above refunds specific line items in a transaction. If you want to refund the entire
transaction, simply provide a reason:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">refund</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Accidental charge</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

For more information on refunds, please consult Paddle’s
refund documentation
.

exclamation

Refunds must always be approved by Paddle before fully processing.

Crediting Transactions

Just like refunding, you can also credit transactions. Crediting transactions will add the funds
to the customer’s balance so it may be used for future purchases. Crediting transactions can
only be done for manually-collected transactions and not for automatically-collected
transactions (like subscriptions) since Paddle handles subscription credits automatically:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$transaction</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">transactions</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Credit a specific line item fully...</span></div><div class="line"><span style="color: #BEC5D4;">$response</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">credit</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Compensation</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pri_123</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

For more info, see
Paddle’s documentation on crediting
.

exclamation

Credits can only be applied for manually-collected transactions. Automatically-collected
transactions are credited by Paddle themselves.

Transactions

You may easily retrieve an array of a billable model‘s transactions via the
transactions property:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$transactions</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->transactions</span><span style="color: #BFC7D5;">;</span></div>

Transactions represent payments for your products and purchases and are accompanied by invoices.
Only completed transactions are stored in your application’s database.

When listing the transactions for a customer, you may use the transaction instance’s methods to
display the relevant payment information. For example, you may wish to list every transaction in
a table, allowing the user to easily download any of the invoices:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">table</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;">    @foreach ($transactions as $transaction)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">tr</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">{{ $transaction->billed_at->toFormattedDateString() }}</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">{{ $transaction->total() }}</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span><span style="color: #BFC7D5;">{{ $transaction->tax() }}</span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">td</span><span style="color: #89DDFF;">><</span><span style="color: #FF5572;">a</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">href</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">{{ <code>route('download-invoice', $transaction->id) }}" target="_blank">Download</spana></spantd>
</spantr>
@endforeach
</spantable>

The download-invoice route may look like the following:

<!-- Syntax highlighted by torchlight.dev --><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;"> Laravel\Paddle\</span><span style="color: #FFCB8B;">Transaction</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;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">/download-invoice/{transaction}</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: #BFC7D5;">, </span><span style="color: #FFCB8B;">Transaction</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$transaction</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;">$transaction</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">redirectToInvoicePdf</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">name</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">download-invoice</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Past and Upcoming
Payments

You may use the lastPayment and nextPayment methods to retrieve and
display a customer’s past or upcoming payments for recurring subscriptions:

<!-- Syntax highlighted by torchlight.dev --><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"> </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;">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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subscription</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">subscription</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lastPayment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lastPayment</span><span style="color: #BFC7D5;">();</span></div><div class="line"><span style="color: #BEC5D4;">$nextPayment</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$subscription</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">nextPayment</span><span style="color: #BFC7D5;">();</span></div>

Both of these methods will return an instance of Laravel\Paddle\Payment; however,
lastPayment will return null when transactions have not been synced by
webhooks yet, while nextPayment will return null when the billing
cycle has ended (such as when a subscription has been canceled):

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">Next payment: </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$nextPayment</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">amount</span><span style="color: #BFC7D5;">() </span><span style="color: #89DDFF;">}}</span><span style="color: #BFC7D5;"> due on </span><span style="color: #89DDFF;">{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$nextPayment</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">date</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">format</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">d/m/Y</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">) </span><span style="color: #89DDFF;">}}</span></div>

Testing

While testing, you should manually test your billing flow to make sure your integration works as
expected.

For automated tests, including those executed within a CI environment, you may use
Laravel’s HTTP Client to fake HTTP calls made to Paddle.
Although this does not test the actual responses from Paddle, it does provide a way to test your
application without actually calling Paddle’s API.


Collections

Collections

Introduction

The Illuminate\Support\Collection class provides a fluent, convenient wrapper for
working with arrays of data. For example, check out the following code. We’ll use the
collect helper to create a new collection instance from the array, run the
strtoupper function on each element, and then remove all empty elements:

<!-- 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><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</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: #89DDFF;">?</span><span style="color: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$name</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;">strtoupper</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: #89DDFF;">-></span><span style="color: #82AAFF;">reject</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;">$name</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;">empty</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></div>

As you can see, the Collection class allows you to chain its methods to perform
fluent mapping and reducing of the underlying array. In general, collections are immutable,
meaning every Collection method returns an entirely new Collection
instance.

Creating Collections

As mentioned above, the collect helper returns a new
Illuminate\Support\Collection instance for the given array. So, creating a
collection is as simple as:

<!-- 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: #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>
lightbulb

The results of Eloquent queries are always returned as
Collection instances.

Extending Collections

Collections are «macroable», which allows you to add additional methods to the
Collection class at run time. The Illuminate\Support\Collection class’
macro method accepts a closure that will be executed when your macro is called. The
macro closure may access the collection’s other methods via $this, just as if it
were a real method of the collection class. For example, the following code adds a
toUpper method to the Collection class:

<!-- 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;">Collection</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;">Str</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">macro</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">toUpper</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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</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: #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;">Str</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">upper</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"><span style="color: #BFC7D5;">});</span></div><div class="line"> </div><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;">first</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;">second</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$upper</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toUpper</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['FIRST', 'SECOND']</span></div>

Typically, you should declare collection macros in the boot method of a service provider.

Macro Arguments

If necessary, you may define macros that accept additional arguments:

<!-- 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;">Collection</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;">Lang</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">macro</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">toLocale</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: #C792EA;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$locale</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: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</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: #D9F5DD;">)</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$locale</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;">Lang</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$value</span><span style="color: #BFC7D5;">, [], </span><span style="color: #BEC5D4;">$locale</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;">$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;">first</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;">second</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$translated</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toLocale</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">es</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>

Available Methods

For the majority of the remaining collection documentation, we’ll discuss each method available
on the Collection class. Remember, all of these methods may be chained to fluently
manipulate the underlying array. Furthermore, almost every method returns a new
Collection instance, allowing you to preserve the original copy of the collection
when necessary:

Method Listing

after()

The after method returns the item after the given item. null is
returned if the given item is not found or is the last item:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</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;"> 4</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</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;"> null</span></div>

This method searches for the given item using «loose» comparison, meaning a string containing an
integer value will be considered equal to an integer of the same value. To use «strict»
comparison, you may provide the strict argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </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;"> null</span></div>

Alternatively, you may provide your own closure to search for the first item that passes a given
truth test:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">after</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;">$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: #C792EA;">></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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 8</span></div>

all()

The all method returns the underlying array represented by the collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

average()

Alias for the avg method.

avg()

The avg method returns the average
value
of a given key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$average</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">avg</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 20</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$average</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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">avg</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>

before()

The before method is the opposite of the after method. It returns the item before the given
item. null is returned if the given item is not found or is the first item:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</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;"> 2</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</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;"> null</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </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;"> null</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">before</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;">$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: #C792EA;">></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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>

chunk()

The chunk method breaks the collection into multiple, smaller collections of a given
size:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]]</span></div>

This method is especially useful in views when working with
a grid system such as Bootstrap.
For example, imagine you have a collection of Eloquent
models you want to display in a grid:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$products</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">) </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">row</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@foreach </span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$chunk</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"><</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;"> </span><span style="color: #FFCB6B;">class</span><span style="color: #89DDFF;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">col-xs-4</span><span style="color: #D9F5DD;">"</span><span style="color: #89DDFF;">>{{</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$product</span><span style="color: #89DDFF;">->name</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">}}</span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">@endforeach</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;"></span</span><span style="color: #FF5572;">div</span><span style="color: #89DDFF;">></span></div><div class="line"><span style="color: #C792EA;">@endforeach</span></div>

chunkWhile()

The chunkWhile method breaks the collection into multiple, smaller collections based
on the evaluation of the given callback. The $chunk variable passed to the closure
may be used to inspect the previous element:

<!-- 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: #89DDFF;">str_split</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">AABBCCCD</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunkWhile</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;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$key</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunk</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</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;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]</span></div>

collapse()

The collapse method collapses a collection of arrays into a single, flat collection:

<!-- 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></div><div class="line"><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: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #F78C6C;">7</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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;">$collapsed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collapse</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collapsed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

collect()

The collect method returns a new Collection instance with the items
currently in the collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collectionA</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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$collectionB</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collectionA</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collectionB</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

The collect method is primarily useful for converting lazy collections into standard Collection
instances:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</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;">yield</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: #C792EA;">yield</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: #C792EA;">yield</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;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</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: #697098;">//</span><span style="color: #697098;"> 'Illuminate\Support\Collection'</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>
lightbulb

The collect method is especially useful when you have an instance of
Enumerable and need a non-lazy collection instance. Since
collect() is part of the Enumerable contract, you can safely use
it to get a Collection instance.

combine()

The combine method combines the values of the collection, as keys, with the values
of another array or collection:

<!-- 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;">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;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$combined</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">combine</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">George</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">29</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$combined</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'George', 'age' => 29]</span></div>

concat()

The concat method appends the given array or collection’s values onto
the end of another collection:

<!-- 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;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$concatenated</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">concat</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">concat</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;">Johnny Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$concatenated</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe', 'Jane Doe', 'Johnny Doe']</span></div>

The concat method numerically reindexes keys for items concatenated onto the
original collection. To maintain keys in associative collections, see the merge method.

contains()

The contains method determines whether the collection contains a given item. You may
pass a closure to the contains method to determine if an element exists in the
collection matching a given truth test:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</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;">5</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>

Alternatively, you may pass a string to the contains method to determine whether the
collection contains a given item value:

<!-- 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;">name</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;">Desk</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;">price</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;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</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;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">New York</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>

You may also pass a key / value pair to the contains method, which will determine if
the given pair exists in the collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">contains</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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>

The contains method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
containsStrict method to filter using «strict»
comparisons.

For the inverse of contains, see the doesntContain method.

containsOneItem()

The containsOneItem method determines whether the collection contains a single item:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</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: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</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;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">1</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;">2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">containsOneItem</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>

containsStrict()

This method has the same signature as the contains
method; however, all values are compared using «strict» comparisons.

lightbulb

This method’s behavior is modified when using Eloquent Collections.

count()

The count method returns the total number of items in the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>

countBy()

The countBy method counts the occurrences of values in the collection. By default,
the method counts the occurrences of every element, allowing you to count certain «types» of
elements in the collection:

<!-- 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: #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;">2</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"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">countBy</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => 1, 2 => 3, 3 => 1]</span></div>

You pass a closure to the countBy method to count all items by a custom value:

<!-- 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;"><a class="__cf_email__" data-cfemail="84e5e8ede7e1c4e3e9e5ede8aae7ebe9" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;"><a class="__cf_email__" data-cfemail="9af8f5f8dae3fbf2f5f5b4f9f5f7" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;"><a class="__cf_email__" data-cfemail="1b787a697774685b7c767a727735787476" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$counted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">countBy</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;">$email</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;">substr</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">strrchr</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">email</span><span style="color: #BFC7D5;">,</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">1</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;">$counted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['gmail.com' => 2, 'yahoo.com' => 1]</span></div>

crossJoin()

The crossJoin method cross joins the collection’s values among the given arrays or
collections, returning a Cartesian product with all possible permutations:

<!-- 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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">crossJoin</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: #BEC5D4;">$matrix</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">$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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">crossJoin</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: #BEC5D4;">$matrix</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

dd()

The dd method dumps the collection’s items and ends execution of the script:

<!-- 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;">John Doe</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;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dd</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;">    Collection {</span></div><div class="line"><span style="color: #697098;">        #items: array:2 [</span></div><div class="line"><span style="color: #697098;">            0 => "John Doe"</span></div><div class="line"><span style="color: #697098;">            1 => "Jane Doe"</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>

If you do not want to stop executing the script, use the dump method instead.

diff()

The diff method compares the collection against another collection or a plain PHP
array based on its values. This method will return the values in the original
collection that are not present in the given collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diff</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 3, 5]</span></div>
lightbulb

This method’s behavior is modified when using Eloquent Collections.

diffAssoc()

The diffAssoc method compares the collection against another collection or a plain
PHP array based on its keys and values. This method will return the key / value
pairs in the original collection that are not present in the given collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</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;">orange</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</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;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">remain</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;">6</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;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffAssoc</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</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;">yellow</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;">type</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;">fruit</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;">remain</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">used</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;">6</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;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['color' => 'orange', 'remain' => 6]</span></div>

diffAssocUsing()

Unlike diffAssoc, diffAssocUsing accepts a user supplied callback
function for the indices comparison:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</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;">orange</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</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;">fruit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">remain</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;">6</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;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffAssocUsing</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Color</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;">yellow</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;">Type</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;">fruit</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;">Remain</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strnatcasecmp</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['color' => 'orange', 'remain' => 6]</span></div>

The callback must be a comparison function that returns an integer less than, equal to, or
greater than zero. For more information, refer to the PHP documentation on array_diff_uassoc,
which is the PHP function that the diffAssocUsing method utilizes internally.

diffKeys()

The diffKeys method compares the collection against another collection or a plain
PHP array based on its keys. This method will return the key / value pairs in the
original collection that are not present in the given collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">one</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;">10</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">two</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;">20</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">three</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;">30</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">four</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;">40</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">five</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;">50</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;">$diff</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">diffKeys</span><span style="color: #BFC7D5;">([</span></div><div class="line"><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></div><div class="line"><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><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">six</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;">6</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eight</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;">8</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;">$diff</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['one' => 10, 'three' => 30, 'five' => 50]</span></div>

doesntContain()

The doesntContain method determines whether the collection does not contain a given
item. You may pass a closure to the doesntContain method to determine if an element
does not exist in the collection matching a given truth test:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</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;">5</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>

Alternatively, you may pass a string to the doesntContain method to determine
whether the collection does not contain a given item value:

<!-- 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;">name</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;">Desk</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;">price</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;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</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"> </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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</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;"> false</span></div>

You may also pass a key / value pair to the doesntContain method, which will
determine if the given pair does not exist in the collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">doesntContain</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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>

The doesntContain method uses «loose» comparisons when checking item values, meaning
a string with an integer value will be considered equal to an integer of the same value.

dot()

The dot method flattens a multi-dimensional collection into a single level
collection that uses «dot» notation to indicate depth:

<!-- 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;">products</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: #D9F5DD;">'</span><span style="color: #C3E88D;">desk</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: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dot</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$flattened</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

dump()

The dump method dumps the collection’s items:

<!-- 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;">John Doe</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;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">dump</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;">    Collection {</span></div><div class="line"><span style="color: #697098;">        #items: array:2 [</span></div><div class="line"><span style="color: #697098;">            0 => "John Doe"</span></div><div class="line"><span style="color: #697098;">            1 => "Jane Doe"</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>

If you want to stop executing the script after dumping the collection, use the dd method instead.

duplicates()

The duplicates method retrieves and returns duplicate values from the collection:

<!-- 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;">a</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;">b</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;">a</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;">c</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;">b</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">duplicates</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2 => 'a', 4 => 'b']</span></div>

If the collection contains arrays or objects, you can pass the key of the attributes that you
wish to check for duplicate values:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$employees</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="016063686660686d416479606c716d642f626e6c" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;">position</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;">Developer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="680209050d1b280d10090518040d460b0705" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;">position</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;">Designer</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="75031c16011a071c1435100d14180519105b161a18" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></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;">position</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;">Developer</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;">$employees</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">duplicates</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">position</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;"> [2 => 'Developer']</span></div>

duplicatesStrict()

This method has the same signature as the duplicates method; however, all values are
compared using «strict» comparisons.

each()

The each method iterates over the items in the collection and passes each item to a
closure:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;">$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: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

If you would like to stop iterating through the items, you may return false from
your closure:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;">$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;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #697098;">/*</span><span style="color: #697098;"> condition </span><span style="color: #697098;">*/</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: #82AAFF;">false</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>

eachSpread()

The eachSpread method iterates over the collection’s items, passing each nested item
value into the given callback:

<!-- 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;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">35</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;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">33</span><span style="color: #BFC7D5;">]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">eachSpread</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;">$name</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$age</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>

You may stop iterating through the items by returning false from the callback:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">eachSpread</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;">$name</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$age</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: #82AAFF;">false</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

ensure()

The ensure method may be used to verify that all elements of a collection are of a
given type or list of types. Otherwise, an UnexpectedValueException will be thrown:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</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><div class="line"> </div><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</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><span style="color: #FFCB8B;">Customer</span><span style="color: #89DDFF;">::</span><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;">]);</span></div>

Primitive types such as string, int, float,
bool, and array may also be specified:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ensure</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">int</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div>
exclamation

The ensure method does not guarantee that elements of different types will not
be added to the collection at a later time.

every()

The every method may be used to verify that all elements of a collection pass a
given truth test:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">every</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;">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: #697098;">//</span><span style="color: #697098;"> false</span></div>

If the collection is empty, the every method will return true:

<!-- 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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">every</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;">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: #697098;">//</span><span style="color: #697098;"> true</span></div>

except()

The except method returns all items in the collection except for those with the
specified keys:

<!-- 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;">product_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">100</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> false</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">except</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: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1]</span></div>

For the inverse of except, see the only method.

lightbulb

This method’s behavior is modified when using Eloquent Collections.

filter()

The filter method filters the collection using the given callback, keeping only
those items that pass a given truth test:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</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;">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;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>

If no callback is supplied, all entries of the collection that are equivalent to
false will be removed:

<!-- 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: #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><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> false</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

For the inverse of filter, see the reject method.

first()

The first method returns the first element in the collection that passes a given
truth test:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</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;">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: #697098;">//</span><span style="color: #697098;"> 3</span></div>

You may also call the first method with no arguments to get the first element in the
collection. If the collection is empty, null is returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">first</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>

firstOrFail()

The firstOrFail method is identical to the first method; however, if no
result is found, an Illuminate\Support\ItemNotFoundException exception will be
thrown:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstOrFail</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;">5</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;"> Throws ItemNotFoundException...</span></div>

You may also call the firstOrFail method with no arguments to get the first element
in the collection. If the collection is empty, an
Illuminate\Support\ItemNotFoundException exception will be thrown:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstOrFail</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Throws ItemNotFoundException...</span></div>

firstWhere()

The firstWhere method returns the first element in the collection with the given key
/ value pair:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Regena</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;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Linda</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;">age</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;">14</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Diego</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;">age</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;">23</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Linda</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;">age</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;">84</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</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;">Linda</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' => 'Linda', 'age' => 14]</span></div>

You may also call the firstWhere method with a comparison operator:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</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: #D9F5DD;">'</span><span style="color: #C3E88D;">>=</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">18</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Diego', 'age' => 23]</span></div>

Like the where method, you may pass one argument to the
firstWhere method. In this scenario, the firstWhere method will return
the first item where the given item key’s value is «truthy»:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">firstWhere</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'Linda', 'age' => 14]</span></div>

flatMap()

The flatMap method iterates through the collection and passes each value to the
given closure. The closure is free to modify the item and return it, thus forming a new
collection of modified items. Then, the array is flattened by one level:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sally</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">school</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;">Arkansas</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">28</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;">$flattened</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">flatMap</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;">$values</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;">array_map</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strtoupper</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;">values</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;">$flattened</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28'];</span></div>

flatten()

The flatten method flattens a multi-dimensional collection into a single dimension:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">taylor</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">languages</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>php', 'javascript'
]
]);
 
$flattened = $collection->flatten();
 
$flattened->all();
 
// ['taylor', 'php', 'javascript'];

If necessary, you may pass the flatten method a «depth» argument:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">iPhone 6S</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Apple</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Samsung</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Galaxy S7</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Samsung</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #82AAFF;"> </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: #BEC5D4;">$products</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">flatten</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: #BEC5D4;">$products</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'iPhone 6S', 'brand' => 'Apple'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Galaxy S7', 'brand' => 'Samsung'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

In this example, calling flatten without providing the depth would have also
flattened the nested arrays, resulting in
['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung']. Providing a depth allows you to
specify the number of levels nested arrays will be flattened.

flip()

The flip method swaps the collection’s keys with their corresponding values:

<!-- 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;">name</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;">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;">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;"><code>laravel']);
 
$flipped = $collection->flip();
 
$flipped->all();
 
// ['taylor' => 'name', 'laravel' => 'framework']

forget()

The forget method removes an item from the collection by its key:

<!-- 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;">name</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;">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;">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;"><code>laravel']);
 
// Forget a single key...
$collection->forget('name');
 
// ['framework' => 'laravel']
 
// Forget multiple keys...
$collection->forget(['name', 'framework']);
 
// []
exclamation

Unlike most other collection methods, forget does not return a new modified
collection; it modifies and returns the collection it is called on.

forPage()

The forPage method returns a new collection containing the items that would be
present on a given page number. The method accepts the page number as its first argument and the
number of items to show per page as its second argument:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</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: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">forPage</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 6]</span></div>

get()

The get method returns the item at a given key. If the key does not exist,
null is returned:

<!-- 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;">name</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;">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;">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;"><code>laravel']);
 
$value = $collection->get('name');
 
// taylor

You may optionally pass a default value as the second argument:

<!-- 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;">name</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;">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;">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;"><code>laravel']);
 
$value = $collection->get('age', 34);
 
// 34

You may even pass a callback as the method’s default value. The result of the callback will be
returned if the specified key does not exist:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</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;">email</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: #D9F5DD;">'</span><span style="color: #C3E88D;"><a class="__cf_email__" data-cfemail="ed998c9481829fad88958c809d8188c38e8280" href="https://laravel.com/cdn-cgi/l/email-protection">[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"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> <a class="__cf_email__" data-cfemail="4a3e2b332625380a2f322b273a262f64292527" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span></div>

groupBy()

The groupBy method groups the collection’s items by a given key:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_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: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x10</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;">product</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;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_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: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x10</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;">product</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;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_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: #D9F5DD;">'</span><span style="color: #C3E88D;">account-x11</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;">product</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;">Desk</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;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        'account-x10' => [</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x10', 'product' => 'Chair'],</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x10', 'product' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        'account-x11' => [</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x11', 'product' => 'Desk'],</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>

Instead of passing a string key, you may pass a callback. The callback should return
the value you wish to key the group by:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</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: #89DDFF;">substr</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;">account_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">-</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;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        'x10' => [</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x10', 'product' => 'Chair'],</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x10', 'product' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        'x11' => [</span></div><div class="line"><span style="color: #697098;">            ['account_id' => 'account-x11', 'product' => 'Desk'],</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>

Multiple grouping criteria may be passed as an array. Each array element will be applied to the
corresponding level within a multi-dimensional array:

<!-- 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: #89DDFF;">new</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">10</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;">user</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;">skill</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;">roles</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;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_3</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">20</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;">user</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;">skill</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;">roles</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;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Role_2</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">30</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;">user</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><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</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;">roles</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;">Role_1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">40</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;">user</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;">4</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</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;">roles</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;">Role_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"> </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: #BEC5D4;">$data</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">groupBy</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">skill</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: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</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;">roles</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">}], preserveKeys: </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></div><div class="line"><span style="color: #697098;">[</span></div><div class="line"><span style="color: #697098;">    1 => [</span></div><div class="line"><span style="color: #697098;">        'Role_1' => [</span></div><div class="line"><span style="color: #697098;">            10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],</span></div><div class="line"><span style="color: #697098;">            20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        'Role_2' => [</span></div><div class="line"><span style="color: #697098;">            20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        'Role_3' => [</span></div><div class="line"><span style="color: #697098;">            10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],</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;">    2 => [</span></div><div class="line"><span style="color: #697098;">        'Role_1' => [</span></div><div class="line"><span style="color: #697098;">            30 => ['user' => 3, 'skill' => 2, 'roles' => ['Role_1']],</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        'Role_2' => [</span></div><div class="line"><span style="color: #697098;">            40 => ['user' => 4, 'skill' => 2, 'roles' => ['Role_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><div class="line"><span style="color: #697098;">*/</span></div>

has()

The has method determines if a given key exists in the collection:

<!-- 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;">account_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</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;">Desk</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;">amount</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;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</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><span style="color: #697098;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</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: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">has</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">amount</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;"> false</span></div>

hasAny()

The hasAny method determines whether any of the given keys exist in the collection:

<!-- 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;">account_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</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;">Desk</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;">amount</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;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasAny</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: #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;"> true</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">hasAny</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;"> false</span></div>

implode()

The implode method joins items in a collection. Its arguments depend on the type of
items in the collection. If the collection contains arrays or objects, you should pass the key
of the attributes you wish to join, and the «glue» string you wish to place between the values:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">account_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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product</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;">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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</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: #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;"> Desk, Chair</span></div>

If the collection contains simple strings or numeric values, you should pass the «glue» as the
only argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</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;"> '1-2-3-4-5'</span></div>

You may pass a closure to the implode method if you would like to format the values
being imploded:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">implode</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: #89DDFF;">strtoupper</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;">product</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;">, </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, CHAIR</span></div>

intersect()

The intersect method removes any values from the original collection that are not
present in the given array or collection. The resulting collection will preserve
the original collection’s keys:

<!-- 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;">Desk</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;">Sofa</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;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersect</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;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [0 => 'Desk', 2 => 'Chair']</span></div>
lightbulb

This method’s behavior is modified when using Eloquent Collections.

intersectAssoc()

The intersectAssoc method compares the original collection against another
collection or array, returning the key / value pairs that are present in all of the
given collections:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</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;">red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">size</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;">M</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">material</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;">cotton</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersectAssoc</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</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;">blue</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;">size</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;">M</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;">material</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;">polyester</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['size' => 'M']</span></div>

intersectByKeys()

The intersectByKeys method removes any keys and their corresponding values from the
original collection that are not present in the given array or collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">serial</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;">UX301</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;">type</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;">screen</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;">year</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;">2009</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;">$intersect</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">intersectByKeys</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">reference</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;">UX404</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</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;">tab</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">year</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;">2011</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;">$intersect</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['type' => 'screen', 'year' => 2009]</span></div>

isEmpty()

The isEmpty method returns true if the collection is empty; otherwise,
false is returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isEmpty</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>

isNotEmpty()

The isNotEmpty method returns true if the collection is not empty;
otherwise, false is returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">isNotEmpty</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>

join()

The join method joins the collection’s values with a string. Using this method’s
second argument, you may also specify how the final element should be appended to the string:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</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: #697098;">//</span><span style="color: #697098;"> 'a, b, c'</span></div><div class="line"><span style="color: #82AAFF;">collect</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: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</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><span style="color: #697098;">//</span><span style="color: #697098;"> 'a, b, and c'</span></div><div class="line"><span style="color: #82AAFF;">collect</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: #82AAFF;"> </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: #82AAFF;">join</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><span style="color: #697098;">//</span><span style="color: #697098;"> 'a and b'</span></div><div class="line"><span style="color: #82AAFF;">collect</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: #82AAFF;">join</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><span style="color: #697098;">//</span><span style="color: #697098;"> 'a'</span></div><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">join</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><span style="color: #697098;">//</span><span style="color: #697098;"> ''</span></div>

keyBy()

The keyBy method keys the collection by the given key. If multiple items have the
same key, only the last one will appear in the new collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keyBy</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: #BEC5D4;">$keyed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

You may also pass a callback to the method. The callback should return the value to key the
collection by:

<!-- Syntax highlighted by torchlight.dev --><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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keyBy</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: #89DDFF;">strtoupper</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;">product_id</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: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

keys()

The keys method returns all of the collection’s keys:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</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: #D9F5DD;">'</span><span style="color: #C3E88D;">product_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: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</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;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</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: #D9F5DD;">'</span><span style="color: #C3E88D;">product_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: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-200</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;">name</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;">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;">$keys</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">keys</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$keys</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['prod-100', 'prod-200']</span></div>

last()

The last method returns the last element in the collection that passes a given truth
test:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</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;">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: #697098;">//</span><span style="color: #697098;"> 2</span></div>

You may also call the last method with no arguments to get the last element in the
collection. If the collection is empty, null is returned:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">last</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 4</span></div>

lazy()

The lazy method returns a new LazyCollection instance from the underlying array
of items:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lazy</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</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: #697098;">//</span><span style="color: #697098;"> Illuminate\Support\LazyCollection</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

This is especially useful when you need to perform transformations on a huge
Collection that contains many items:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$count</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$hugeCollection</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">lazy</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">country</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </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"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">balance</span><span style="color: #D9F5DD;">'</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;">100</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">();</span></div>

By converting the collection to a LazyCollection, we avoid having to allocate a ton
of additional memory. Though the original collection still keeps its values in memory,
the subsequent filters will not. Therefore, virtually no additional memory will be allocated
when filtering the collection’s results.

macro()

The static macro method allows you to add methods to the Collection
class at run time. Refer to the documentation on extending
collections
for more information.

make()

The static make method creates a new collection instance. See the Creating Collections section.

map()

The map method iterates through the collection and passes each value to the given
callback. The callback is free to modify the item and return it, thus forming a new collection
of modified items:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$multiplied</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</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;">$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: #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;">$multiplied</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 6, 8, 10]</span></div>
exclamation

Like most other collection methods, map returns a new collection instance; it
does not modify the collection it is called on. If you want to transform the original
collection, use the transform method.

mapInto()

The mapInto() method iterates over the collection, creating a new instance of the
given class by passing the value into the constructor:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">Currency</span></div><div class="line"><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: #697098;">     * Create a new currency instance.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</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;">string</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$code</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </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;">$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;">USD</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;">EUR</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;">GBP</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$currencies</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapInto</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">Currency</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: #BEC5D4;">$currencies</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [Currency('USD'), Currency('EUR'), Currency('GBP')]</span></div>

mapSpread()

The mapSpread method iterates over the collection’s items, passing each nested item
value into the given closure. The closure is free to modify the item and return it, thus forming
a new collection of modified items:

<!-- 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: #F78C6C;">0</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</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: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">chunk</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: #BEC5D4;">$sequence</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapSpread</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: #BEC5D4;">$sequence</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1, 5, 9, 13, 17]</span></div>

mapToGroups()

The mapToGroups method groups the collection’s items by the given closure. The
closure should return an associative array containing a single key / value pair, thus forming a
new collection of grouped values:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</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;">Sales</span><span style="color: #D9F5DD;">'</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: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Jane Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</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;">Sales</span><span style="color: #D9F5DD;">'</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: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Johnny Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</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;">Marketing</span><span style="color: #D9F5DD;">'</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: #BEC5D4;">$grouped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapToGroups</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;">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: #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: #BEC5D4;">$grouped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        'Sales' => ['John Doe', 'Jane Doe'],</span></div><div class="line"><span style="color: #697098;">        'Marketing' => ['Johnny Doe'],</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;">$grouped</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;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe', 'Jane Doe']</span></div>

mapWithKeys()

The mapWithKeys method iterates through the collection and passes each value to the
given callback. The callback should return an associative array containing a single key / value
pair:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</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;">Sales</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</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;"><a class="__cf_email__" data-cfemail="c3a9acabad83a6bba2aeb3afa6eda0acae" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</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: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Jane</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">department</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;">Marketing</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">email</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;"><a class="__cf_email__" data-cfemail="402a212e25002538212d302c256e232f2d" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</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: #BEC5D4;">$keyed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mapWithKeys</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: #BEC5D4;">$keyed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        '<a class="__cf_email__" data-cfemail="6e040106002e0b160f031e020b400d0103" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>' => 'John',</span></div><div class="line"><span style="color: #697098;">        '<a class="__cf_email__" data-cfemail="f399929d96b3968b929e839f96dd909c9e" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>' => 'Jane',</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

max()

The max method returns the maximum value of a given key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$max</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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;">max</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 20</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$max</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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">max</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>

median()

The median method returns the median
value
of a given key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$median</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">median</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 15</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$median</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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">median</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1.5</span></div>

merge()

The merge method merges the given array or collection with the original collection.
If a string key in the given items matches a string key in the original collection, the given
item’s value will overwrite the value in the original collection:

<!-- 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;">product_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</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;">200</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</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;">false</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'price' => 200, 'discount' => false]</span></div>

If the given item’s keys are numeric, the values will be appended to the end of the collection:

<!-- 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;">Desk</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;">Chair</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</span><span style="color: #BFC7D5;">([</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Desk', 'Chair', 'Bookcase', 'Door']</span></div>

mergeRecursive()

The mergeRecursive method merges the given array or collection recursively with the
original collection. If a string key in the given items matches a string key in the original
collection, then the values for these keys are merged together into an array, and this is done
recursively:

<!-- 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;">product_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">100</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mergeRecursive</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: #F78C6C;">2</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;">200</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</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;">false</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$merged</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false]</span></div>

min()

The min method returns the minimum value of a given key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$min</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;">foo</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;">10</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;">foo</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;">20</span><span style="color: #BFC7D5;">]])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">min</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></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;">$min</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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">min</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>

mode()

The mode method returns the mode value of a given key:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$mode</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">20</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">40</span><span style="color: #BFC7D5;">]</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mode</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></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;">$mode</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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">mode</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$mode</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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</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: #89DDFF;">-></span><span style="color: #82AAFF;">mode</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]</span></div>

multiply()

The multiply method creates the specified number of copies of all items in the
collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User #1</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;">email</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;"><a class="__cf_email__" data-cfemail="fa8f899f88cbba9f829b978a969fd4999597" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">User #2</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;">email</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;"><a class="__cf_email__" data-cfemail="b9cccadccb8bf9dcc1d8d4c9d5dc97dad6d4" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a></span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">multiply</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></div><div class="line"><span style="color: #697098;">    [</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="1f6a6c7a6d2e5f7a677e726f737a317c7072" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="33464056410173564b525e435f561d505c5e" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="47323422357607223f262a372b226924282a" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="82f7f1e7f0b0c2e7fae3eff2eee7ace1edef" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #1', 'email' => '<a class="__cf_email__" data-cfemail="3d484e584f0c7d58455c504d5158135e5250" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'User #2', 'email' => '<a class="__cf_email__" data-cfemail="7c090f190e4e3c19041d110c1019521f1311" href="https://laravel.com/cdn-cgi/l/email-protection">[email protected]</a>'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

nth()

The nth method creates a new collection consisting of every n-th element:

<!-- 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;">a</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;">b</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;">c</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;">d</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;">e</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;">f</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">nth</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['a', 'e']</span></div>

You may optionally pass a starting offset as the second argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">nth</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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;"> ['b', 'f']</span></div>

only()

The only method returns the items in the collection with the specified keys:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">product_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><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">100</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">discount</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> false</span></div><div class="line"><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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">only</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><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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'name' => 'Desk']</span></div>

For the inverse of only, see the except method.

lightbulb

This method’s behavior is modified when using Eloquent Collections.

pad()

The pad method will fill the array with the given value until the array reaches the
specified size. This method behaves like the array_pad PHP function.

To pad to the left, you should specify a negative size. No padding will take place if the
absolute value of the given size is less than or equal to the length of the array:

<!-- 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;">A</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;">B</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;">C</span><span style="color: #D9F5DD;">'</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pad</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">5</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['A', 'B', 'C', 0, 0]</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pad</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">-</span><span style="color: #F78C6C;">5</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 'A', 'B', 'C']</span></div>

partition()

The partition method may be combined with PHP array destructuring to separate
elements that pass a given truth test from those that do not:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$underThree</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$equalOrAboveThree</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">partition</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;">$i</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;">$i</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;"><</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;">$underThree</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$equalOrAboveThree</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5, 6]</span></div>

percentage()

The percentage method may be used to quickly determine the percentage of items in
the collection that pass a given truth test:

<!-- 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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</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;">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"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">percentage</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: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</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;">1</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 33.33</span></div>

By default, the percentage will be rounded to two decimal places. However, you may customize this
behavior by providing a second argument to the method:

<!-- Syntax highlighted by torchlight.dev --><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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">percentage</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: #BEC5D4;">$value</span><span style="color: #D9F5DD;">)</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;">1</span><span style="color: #BFC7D5;">, precision: </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;"> 33.333</span></div>

pipe()

The pipe method passes the collection to the given closure and returns the result of
the executed closure:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$piped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipe</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</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;"> 6</span></div>

pipeInto()

The pipeInto method creates a new instance of the given class and passes the
collection into the constructor:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">class</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB6B;">ResourceCollection</span></div><div class="line"><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: #697098;">     * Create a new ResourceCollection instance.</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</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: #89DDFF;">__construct</span><span style="color: #D9F5DD;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">public</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </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;">$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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$resource</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipeInto</span><span style="color: #BFC7D5;">(</span><span style="color: #FFCB8B;">ResourceCollection</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: #BEC5D4;">$resource</span><span style="color: #89DDFF;">->collection-></span><span style="color: #82AAFF;">all</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]</span></div>

pipeThrough()

The pipeThrough method passes the collection to the given array of closures and
returns the result of the executed closures:

<!-- 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;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><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: #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"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pipeThrough</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</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"><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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</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: #697098;">//</span><span style="color: #697098;"> 15</span></div>

pluck()

The pluck method retrieves all of the values for a given key:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">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;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</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: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Desk', 'Chair']</span></div>

You may also specify how you wish the resulting collection to be keyed:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</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;">product_id</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['prod-100' => 'Desk', 'prod-200' => 'Chair']</span></div>

The pluck method also supports retrieving nested values using «dot» notation:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Laracon</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first_day</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Rosa</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;">Judith</span><span style="color: #D9F5DD;">'</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: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">VueConf</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first_day</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail</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;">Joey</span><span style="color: #D9F5DD;">'</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: #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: #BEC5D4;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">speakers.first_day</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['Rosa', 'Judith'], ['Abigail', 'Joey']]</span></div>

If duplicate keys exist, the last matching element will be inserted into the plucked collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Tesla</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;">color</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;">red</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Pagani</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;">color</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;">white</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Tesla</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;">color</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;">black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</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;">Pagani</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;">color</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;">orange</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;">$plucked</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pluck</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">color</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$plucked</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Tesla' => 'black', 'Pagani' => 'orange']</span></div>

pop()

The pop method removes and returns the last item from the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pop</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><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

You may pass an integer to the pop method to remove and return multiple items from
the end of a collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pop</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;"> collect([5, 4, 3])</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

prepend()

The prepend method adds an item to the beginning of the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">prepend</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 3, 4, 5]</span></div>

You may also pass a second argument to specify the key of the prepended item:

<!-- 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;">one</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: #D9F5DD;">'</span><span style="color: #C3E88D;">two</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;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">prepend</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">0</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['zero' => 0, 'one' => 1, 'two' => 2]</span></div>

pull()

The pull method removes and returns an item from the collection by its key:

<!-- 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;">product_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: #D9F5DD;">'</span><span style="color: #C3E88D;">prod-100</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;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">pull</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;"> 'Desk'</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 'prod-100']</span></div>

push()

The push method appends an item to the end of the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

put()

The put method sets the given key and value in the collection:

<!-- 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;">product_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;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</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;">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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product_id' => 1, 'name' => 'Desk', 'price' => 100]</span></div>

random()

The random method returns a random item from the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</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>

You may pass an integer to random to specify how many items you would like to
randomly retrieve. A collection of items is always returned when explicitly passing the number
of items you wish to receive:

<!-- Syntax highlighted by torchlight.dev --><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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</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: #BEC5D4;">$random</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 5] - (retrieved randomly)</span></div>

If the collection instance has fewer items than requested, the random method will
throw an InvalidArgumentException.

The random method also accepts a closure, which will receive the current collection
instance:

<!-- 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;">Collection</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">random</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$items</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #89DDFF;">min</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">items</span><span style="color: #BFC7D5;">)));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$random</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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] - (retrieved randomly)</span></div>

range()

The range method returns a collection containing integers between the specified
range:

<!-- 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: #89DDFF;">-></span><span style="color: #82AAFF;">range</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5, 6]</span></div>

reduce()

The reduce method reduces the collection to a single value, passing the result of
each iteration into the subsequent iteration:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$total</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</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: #89DDFF;">?</span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$carry</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</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;">$carry</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></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;"> 6</span></div>

The value for $carry on the first iteration is null; however, you may
specify its initial value by passing a second argument to reduce:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</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;">$carry</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">int</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$item</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;">$carry</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></div><div class="line"><span style="color: #BFC7D5;">}, </span><span style="color: #F78C6C;">4</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>

The reduce method also passes array keys in associative collections to the given
callback:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">usd</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;">1400</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">gbp</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;">1200</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eur</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;">1000</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;">$ratio</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;">usd</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></div><div class="line"><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: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">1.37</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">eur</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.22</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduce</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;">$carry</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><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #BEC5D4;">$ratio</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;">$carry</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: #BEC5D4;">$ratio</span><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$key</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;"> 4264</span></div>

reduceSpread()

The reduceSpread method reduces the collection to an array of values, passing the
results of each iteration into the subsequent iteration. This method is similar to the
reduce method; however, it can accept multiple initial values:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Image</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">status</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">unprocessed</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reduceSpread</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;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Image</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">>=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">creditsRequired</span><span style="color: #BFC7D5;">()) {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$batch</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$image</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">-=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$image</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">creditsRequired</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: #BFC7D5;"> </span><span style="color: #C792EA;">return</span><span style="color: #BFC7D5;"> [</span><span style="color: #BEC5D4;">$creditsRemaining</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$batch</span><span style="color: #BFC7D5;">];</span></div><div class="line"><span style="color: #BFC7D5;">    }, </span><span style="color: #BEC5D4;">$creditsAvailable</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">());</span></div>

reject()

The reject method filters the collection using the given closure. The closure should
return true if the item should be removed from the resulting collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reject</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;">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;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

For the inverse of the reject method, see the filter method.

replace()

The replace method behaves similarly to merge; however, in addition to
overwriting matching items that have string keys, the replace method will also
overwrite items in the collection that have matching numeric keys:

<!-- 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><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">James</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">replace</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria</span><span style="color: #D9F5DD;">'</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: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Finn</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$replaced</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Taylor', 'Victoria', 'James', 'Finn']</span></div>

replaceRecursive()

This method works like replace, but it will recur into arrays and apply the same
replacement process to the inner values:

<!-- 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></div><div class="line"><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"><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><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">James</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Finn</span><span style="color: #D9F5DD;">'</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: #BEC5D4;">$replaced</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">replaceRecursive</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Charlie</span><span style="color: #D9F5DD;">'</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: #89DDFF;">=></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: #D9F5DD;">'</span><span style="color: #C3E88D;">King</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;">$replaced</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Charlie', 'Abigail', ['James', 'King', 'Finn']]</span></div>

reverse()

The reverse method reverses the order of the collection’s items, preserving the
original keys:

<!-- 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;">a</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;">b</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;">c</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;">d</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;">e</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$reversed</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">reverse</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$reversed</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        4 => 'e',</span></div><div class="line"><span style="color: #697098;">        3 => 'd',</span></div><div class="line"><span style="color: #697098;">        2 => 'c',</span></div><div class="line"><span style="color: #697098;">        1 => 'b',</span></div><div class="line"><span style="color: #697098;">        0 => 'a',</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The search method searches the collection for the given value and returns its key if
found. If the item is not found, false is returned:

<!-- 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: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>

The search is done using a «loose» comparison, meaning a string with an integer value will be
considered equal to an integer of the same value. To use «strict» comparison, pass
true as the second argument to the method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">4</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, strict: </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;"> false</span></div>

Alternatively, you may provide your own closure to search for the first item that passes a given
truth test:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">8</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">search</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;">$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: #C792EA;">></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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div>

select()

The select method selects the given keys from the collection, similar to an SQL
SELECT statement:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</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;">role</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;">Developer</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;">status</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;">active</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Victoria Faith</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;">role</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;">Researcher</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;">status</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;">active</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;">$users</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">select</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;">role</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;">        ['name' => 'Taylor Otwell', 'role' => 'Developer'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Victoria Faith', 'role' => 'Researcher'],</span></div><div class="line"><span style="color: #697098;">    ],</span></div><div class="line"><span style="color: #697098;">*/</span></div>

shift()

The shift method removes and returns the first item from the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shift</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 3, 4, 5]</span></div>

You may pass an integer to the shift method to remove and return multiple items from
the beginning of a collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shift</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;"> collect([1, 2, 3])</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

shuffle()

The shuffle method randomly shuffles the items in the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$shuffled</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">shuffle</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$shuffled</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

skip()

The skip method returns a new collection, with the given number of elements removed
from the beginning of the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</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: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</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: #BEC5D4;">$collection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skip</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6, 7, 8, 9, 10]</span></div>

skipUntil()

The skipUntil method skips over items from the collection until the given callback
returns true and then returns the remaining items in the collection as a new
collection instance:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipUntil</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;">$item</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: #C792EA;">>=</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;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>

You may also pass a simple value to the skipUntil method to skip all items until the
given value is found:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipUntil</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: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4]</span></div>
exclamation

If the given value is not found or the callback never returns true, the
skipUntil method will return an empty collection.

skipWhile()

The skipWhile method skips over items from the collection while the given callback
returns true and then returns the remaining items in the collection as a new
collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">skipWhile</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;">$item</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: #C792EA;"><=</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;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [4]</span></div>
exclamation

If the callback never returns false, the skipWhile method will
return an empty collection.

slice()

The slice method returns a slice of the collection starting at the given index:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</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: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</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: #BEC5D4;">$slice</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">slice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$slice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6, 7, 8, 9, 10]</span></div>

If you would like to limit the size of the returned slice, pass the desired size as the second
argument to the method:

<!-- Syntax highlighted by torchlight.dev --><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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">slice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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: #BEC5D4;">$slice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 6]</span></div>

The returned slice will preserve keys by default. If you do not wish to preserve the original
keys, you can use the values method to reindex them.

sliding()

The sliding method returns a new collection of chunks representing a «sliding
window» view of the items in the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</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: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</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], [2, 3], [3, 4], [4, 5]]</span></div>

This is especially useful in conjunction with the eachSpread method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$transactions</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</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;">eachSpread</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$previous</span><span style="color: #BFC7D5;">, </span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #89DDFF;">->total</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$previous</span><span style="color: #89DDFF;">->total</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">+</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$current</span><span style="color: #89DDFF;">->amount</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

You may optionally pass a second «step» value, which determines the distance between the first
item of every chunk:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sliding</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">, step: </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunks</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</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], [3, 4, 5]]</span></div>

sole()

The sole method returns the first element in the collection that passes a given
truth test, but only if the truth test matches exactly one element:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</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;">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: #697098;">//</span><span style="color: #697098;"> 2</span></div>

You may also pass a key / value pair to the sole method, which will return the first
element in the collection that matches the given pair, but only if it exactly one element
matches:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</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: #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: #697098;">//</span><span style="color: #697098;"> ['product' => 'Chair', 'price' => 100]</span></div>

Alternatively, you may also call the sole method with no argument to get the first
element in the collection if there is only one element:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sole</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['product' => 'Desk', 'price' => 200]</span></div>

If there are no elements in the collection that should be returned by the sole
method, an \Illuminate\Collections\ItemNotFoundException exception will be thrown.
If there is more than one element that should be returned, an
\Illuminate\Collections\MultipleItemsFoundException will be thrown.

some()

Alias for the contains method.

sort()

The sort method sorts the collection. The sorted collection keeps the original array
keys, so in the following example we will use the values method to reset the keys to consecutively
numbered indexes:

<!-- 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: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

If your sorting needs are more advanced, you may pass a callback to sort with your
own algorithm. Refer to the PHP documentation on uasort,
which is what the collection’s sort method calls utilizes internally.

lightbulb

If you need to sort a collection of nested arrays or objects, see the sortBy and sortByDesc methods.

sortBy()

The sortBy method sorts the collection by the given key. The sorted collection keeps
the original array keys, so in the following example we will use the values method to reset the keys to consecutively
numbered indexes:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">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: #BEC5D4;">$sorted</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</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: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The sortBy method accepts sort flags as its second
argument:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</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;">Item 1</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</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;">Item 12</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</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;">Item 3</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">title</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">SORT_NATURAL</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['title' => 'Item 1'],</span></div><div class="line"><span style="color: #697098;">        ['title' => 'Item 3'],</span></div><div class="line"><span style="color: #697098;">        ['title' => 'Item 12'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

Alternatively, you may pass your own closure to determine how to sort the collection’s values:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</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;">Mahogany</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Red</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;">Beige</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;">Brown</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</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;">$product</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;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">product</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</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: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Chair', 'colors' => ['Black']],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

If you would like to sort your collection by multiple attributes, you may pass an array of sort
operations to the sortBy method. Each sort operation should be an array consisting
of the attribute that you wish to sort by and the direction of the desired sort:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</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;">age</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;">34</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</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;">age</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;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</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;">age</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;">36</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</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;">age</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;">32</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</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: #D9F5DD;">'</span><span style="color: #C3E88D;">asc</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;">age</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">desc</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: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Abigail Otwell', 'age' => 32],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Abigail Otwell', 'age' => 30],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Taylor Otwell', 'age' => 36],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Taylor Otwell', 'age' => 34],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

When sorting a collection by multiple attributes, you may also provide closures that define each
sort operation:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</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;">age</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;">34</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</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;">age</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;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Taylor Otwell</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;">age</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;">36</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Abigail Otwell</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;">age</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;">32</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortBy</span><span style="color: #BFC7D5;">([</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">fn</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;">$a</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$a</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: #C792EA;"><=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</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><span style="color: #C792EA;">fn</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;">$a</span><span style="color: #BFC7D5;">, </span><span style="color: #C792EA;">array</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$b</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$b</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: #C792EA;"><=></span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$a</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></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: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Abigail Otwell', 'age' => 32],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Abigail Otwell', 'age' => 30],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Taylor Otwell', 'age' => 36],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Taylor Otwell', 'age' => 34],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

sortByDesc()

This method has the same signature as the sortBy
method, but will sort the collection in the opposite order.

sortDesc()

This method will sort the collection in the opposite order as the sort method:

<!-- 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: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortDesc</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [5, 4, 3, 2, 1]</span></div>

Unlike sort, you may not pass a closure to sortDesc. Instead, you
should use the sort method and invert your comparison.

sortKeys()

The sortKeys method sorts the collection by the keys of the underlying associative
array:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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;">22345</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</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;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">last</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;">Doe</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortKeys</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$sorted</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        'first' => 'John',</span></div><div class="line"><span style="color: #697098;">        'id' => 22345,</span></div><div class="line"><span style="color: #697098;">        'last' => 'Doe',</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

sortKeysDesc()

This method has the same signature as the sortKeys
method, but will sort the collection in the opposite order.

sortKeysUsing()

The sortKeysUsing method sorts the collection by the keys of the underlying
associative array using a callback:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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;">22345</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">first</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;">John</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">last</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;">Doe</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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sortKeysUsing</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">strnatcasecmp</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: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        'first' => 'John',</span></div><div class="line"><span style="color: #697098;">        'ID' => 22345,</span></div><div class="line"><span style="color: #697098;">        'last' => 'Doe',</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The callback must be a comparison function that returns an integer less than, equal to, or
greater than zero. For more information, refer to the PHP documentation on uksort,
which is the PHP function that sortKeysUsing method utilizes internally.

splice()

The splice method removes and returns a slice of items starting at the specified
index:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3, 4, 5]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

You may pass a second argument to limit the size of the resulting collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 4, 5]</span></div>

In addition, you may pass a third argument containing the new items to replace the items removed
from the collection:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splice</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">, [</span><span style="color: #F78C6C;">10</span><span style="color: #BFC7D5;">, </span><span style="color: #F78C6C;">11</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [3]</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 10, 11, 4, 5]</span></div>

split()

The split method breaks a collection into the given number of groups:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$groups</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">split</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: #BEC5D4;">$groups</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]]</span></div>

splitIn()

The splitIn method breaks a collection into the given number of groups, filling
non-terminal groups completely before allocating the remainder to the final group:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">6</span><span style="color: #BFC7D5;">,</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: #F78C6C;">8</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">9</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: #BEC5D4;">$groups</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">splitIn</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: #BEC5D4;">$groups</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 10]]</span></div>

sum()

The sum method returns the sum of all items in the collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">])</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 15</span></div>

If the collection contains nested arrays or objects, you should pass a key that will be used to
determine which values to sum:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">JavaScript: The Good Parts</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;">pages</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;">176</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">JavaScript: The Definitive Guide</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;">pages</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;">1096</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">pages</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;"> 1272</span></div>

In addition, you may pass your own closure to determine which values of the collection to sum:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Black</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;">Mahogany</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">colors</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: #D9F5DD;">'</span><span style="color: #C3E88D;">Red</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;">Beige</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;">Brown</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sum</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;">$product</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;">count</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">product</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">colors</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><span style="color: #697098;"> 6</span></div>

take()

The take method returns a new collection with the specified number of items:

<!-- 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: #F78C6C;">0</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

You may also pass a negative integer to take the specified number of items from the end of the
collection:

<!-- 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: #F78C6C;">0</span><span style="color: #BFC7D5;">,</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;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </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: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</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: #BEC5D4;">$chunk</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

takeUntil()

The takeUntil method returns items in the collection until the given callback
returns true:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntil</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;">$item</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: #C792EA;">>=</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;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

You may also pass a simple value to the takeUntil method to get the items until the
given value is found:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntil</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: #BEC5D4;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>
exclamation

If the given value is not found or the callback never returns true, the
takeUntil method will return all items in the collection.

takeWhile()

The takeWhile method returns items in the collection until the given callback
returns false:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$subset</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeWhile</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;">$item</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: #C792EA;"><</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;">$subset</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>
exclamation

If the callback never returns false, the takeWhile method will
return all items in the collection.

tap()

The tap method passes the collection to the given callback, allowing you to «tap»
into the collection at a specific point and do something with the items while not affecting the
collection itself. The collection is then returned by the tap method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #82AAFF;">collect</span><span style="color: #BFC7D5;">([</span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">3</span><span style="color: #BFC7D5;">,</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;">5</span><span style="color: #BFC7D5;">])</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">sort</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Log</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">debug</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Values after sorting</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">shift</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div>

times()

The static times method creates a new collection by invoking the given closure a
specified number of times:

<!-- 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: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">10</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;">$number</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;">$number</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">*</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]</span></div>

toArray()

The toArray method converts the collection into a plain PHP array. If
the collection’s values are Eloquent models, the
models will also be converted to arrays:

<!-- 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;">name</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;">Desk</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;">price</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;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</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;">        ['name' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>
exclamation

toArray also converts all of the collection’s nested objects that are an
instance of Arrayable to an array. If you want to get the raw array underlying
the collection, use the all method instead.

toJson()

The toJson method converts the collection into a JSON serialized string:

<!-- 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;">name</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;">Desk</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;">price</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;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toJson</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":200}'</span></div>

transform()

The transform method iterates over the collection and calls the given callback with
each item in the collection. The items in the collection will be replaced by the values returned
by the callback:

<!-- 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: #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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">5</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">transform</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;">$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: #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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [2, 4, 6, 8, 10]</span></div>
exclamation

Unlike most other collection methods, transform modifies the collection itself.
If you wish to create a new collection instead, use the map method.

undot()

The undot method expands a single-dimensional collection that uses «dot» notation
into a multi-dimensional collection:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$person</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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name.first_name</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;">Marie</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">name.last_name</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;">Valentine</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.line_1</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;">2992 Eagle Drive</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.line_2</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: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.suburb</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;">Detroit</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.state</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;">MI</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">address.postcode</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;">48219</span><span style="color: #D9F5DD;">'</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$person</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$person</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">undot</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$person</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</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;">        "name" => [</span></div><div class="line"><span style="color: #697098;">            "first_name" => "Marie",</span></div><div class="line"><span style="color: #697098;">            "last_name" => "Valentine",</span></div><div class="line"><span style="color: #697098;">        ],</span></div><div class="line"><span style="color: #697098;">        "address" => [</span></div><div class="line"><span style="color: #697098;">            "line_1" => "2992 Eagle Drive",</span></div><div class="line"><span style="color: #697098;">            "line_2" => "",</span></div><div class="line"><span style="color: #697098;">            "suburb" => "Detroit",</span></div><div class="line"><span style="color: #697098;">            "state" => "MI",</span></div><div class="line"><span style="color: #697098;">            "postcode" => "48219",</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>

union()

The union method adds the given array to the collection. If the given array contains
keys that are already in the original collection, the original collection’s values will be
preferred:

<!-- 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: #F78C6C;">1</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #BEC5D4;">$union</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">union</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: #BFC7D5;"> [</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">c</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: #D9F5DD;">'</span><span style="color: #C3E88D;">d</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$union</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [1 => ['a'], 2 => ['b'], 3 => ['c']]</span></div>

unique()

The unique method returns all of the unique items in the collection. The returned
collection keeps the original array keys, so in the following example we will use the values method to reset the keys to consecutively
numbered indexes:

<!-- 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: #F78C6C;">1</span><span style="color: #BFC7D5;">,</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;">2</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><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">,</span><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">2</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

When dealing with nested arrays or objects, you may specify the key used to determine uniqueness:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">iPhone 6</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;">brand</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;">Apple</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;">type</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;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">iPhone 5</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;">brand</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;">Apple</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;">type</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;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Apple Watch</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;">brand</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;">Apple</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;">type</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;">watch</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Galaxy S6</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;">brand</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;">Samsung</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;">type</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;">phone</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Galaxy Gear</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;">brand</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;">Samsung</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;">type</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;">watch</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;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

Finally, you may also pass your own closure to the unique method to specify which
value should determine an item’s uniqueness:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$unique</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unique</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: #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;">brand</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$item</span><span style="color: #BFC7D5;">[</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">type</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;">$unique</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The unique method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
uniqueStrict method to filter using «strict»
comparisons.

lightbulb

This method’s behavior is modified when using Eloquent Collections.

uniqueStrict()

This method has the same signature as the unique
method; however, all values are compared using «strict» comparisons.

unless()

The unless method will execute the given callback unless the first argument given to
the method evaluates to true:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 5]</span></div>

A second callback may be passed to the unless method. The second callback will be
executed when the first argument given to the unless method evaluates to
true:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">unless</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">true</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 5]</span></div>

For the inverse of unless, see the when
method.

unlessEmpty()

Alias for the whenNotEmpty method.

unlessNotEmpty()

Alias for the whenEmpty method.

unwrap()

The static unwrap method returns the collection’s underlying items from the given
value when applicable:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</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;">John Doe</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;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">unwrap</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 'John Doe'</span></div>

value()

The value method retrieves a given value from the first element of the collection:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Speaker</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;">price</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;">400</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;">$value</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">value</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;"> 200</span></div>

values()

The values method returns a new collection with the keys reset to consecutive
integers:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">10</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #F78C6C;">11</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</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;">$values</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">values</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$values</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        0 => ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">        1 => ['product' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

when()

The when method will execute the given callback when the first argument given to the
method evaluates to true. The collection instance and the first argument given to
the when method will be provided to the closure:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></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: #C792EA;">function</span><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">(</span><span style="color: #FFCB8B;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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: #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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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: #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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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]</span></div>

A second callback may be passed to the when method. The second callback will be
executed when the first argument given to the when method evaluates to
false:

<!-- 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: #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"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">when</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">false</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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: #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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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, 5]</span></div>

For the inverse of when, see the unless
method.

whenEmpty()

The whenEmpty method will execute the given callback when the collection is empty:

<!-- 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;">Michael</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;">Tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Michael', 'Tom']</span></div><div class="line"> </div><div class="line"> </div><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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Adam']</span></div>

A second closure may be passed to the whenEmpty method that will be executed when
the collection is not empty:

<!-- 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;">Michael</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;">Tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Adam</span><span style="color: #D9F5DD;">'</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['Michael', 'Tom', 'Taylor']</span></div>

For the inverse of whenEmpty, see the whenNotEmpty method.

whenNotEmpty()

The whenNotEmpty method will execute the given callback when the collection is not
empty:

<!-- 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;">michael</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;">tom</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['michael', 'tom', 'adam']</span></div><div class="line"> </div><div class="line"> </div><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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

A second closure may be passed to the whenNotEmpty method that will be executed when
the collection is empty:

<!-- 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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whenNotEmpty</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">adam</span><span style="color: #D9F5DD;">'</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;">Collection</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</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;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">push</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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>

For the inverse of whenNotEmpty, see the whenEmpty method.

where()

The where method filters the collection by a given key / value pair:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</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;">price</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;">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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The where method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereStrict method to filter using «strict»
comparisons.

Optionally, you may pass a comparison operator as the second parameter. Supported operators are:
‘===’, ‘!==’, ‘!=’, ‘==’, ‘=’, ‘<>’, ‘>’, ‘<‘, ‘>=’, and ‘<=’:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Jim</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;">deleted_at</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;">2019-01-01 00:00:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sally</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;">deleted_at</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;">2019-01-02 00:00:00</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Sue</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;">deleted_at</span><span style="color: #D9F5DD;">'</span><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">deleted_at</span><span style="color: #D9F5DD;">'</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: #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: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

whereStrict()

This method has the same signature as the where method;
however, all values are compared using «strict» comparisons.

whereBetween()

The whereBetween method filters the collection by determining if a specified item
value is within a given range:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">80</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pencil</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;">price</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;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</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;">price</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;">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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereBetween</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><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

whereIn()

The whereIn method removes elements from the collection that do not have a specified
item value that is contained within the given array:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</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;">price</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;">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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereIn</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;">150</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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' => 'Desk', 'price' => 200],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Bookcase', 'price' => 150],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The whereIn method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereInStrict method to filter using «strict»
comparisons.

whereInStrict()

This method has the same signature as the whereIn
method; however, all values are compared using «strict» comparisons.

whereInstanceOf()

The whereInstanceOf method filters the collection by a given class type:

<!-- Syntax highlighted by torchlight.dev --><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;"> App\Models\</span><span style="color: #FFCB8B;">Post</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><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></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #BFC7D5;">,</span></div><div class="line"><span style="color: #82AAFF;"> </span><span style="color: #89DDFF;">new</span><span style="color: #82AAFF;"> </span><span style="color: #FFCB8B;">Post</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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereInstanceOf</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><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [App\Models\User, App\Models\User]</span></div>

whereNotBetween()

The whereNotBetween method filters the collection by determining if a specified item
value is outside of a given range:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">80</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Pencil</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;">price</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;">30</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</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;">price</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;">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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotBetween</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><span style="color: #F78C6C;">200</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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' => 'Chair', 'price' => 80],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Pencil', 'price' => 30],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

whereNotIn()

The whereNotIn method removes elements from the collection that have a specified
item value that is contained within the given array:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">price</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;">200</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Chair</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;">price</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;">100</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">price</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;">150</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Door</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;">price</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;">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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotIn</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;">150</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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' => 'Chair', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">        ['product' => 'Door', 'price' => 100],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

The whereNotIn method uses «loose» comparisons when checking item values, meaning a
string with an integer value will be considered equal to an integer of the same value. Use the
whereNotInStrict method to filter using
«strict» comparisons.

whereNotInStrict()

This method has the same signature as the whereNotIn method; however, all values are
compared using «strict» comparisons.

whereNotNull()

The whereNotNull method returns items from the collection where the given key is not
null:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNotNull</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => 'Desk'],</span></div><div class="line"><span style="color: #697098;">        ['name' => 'Bookcase'],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

whereNull()

The whereNull method returns items from the collection where the given key is
null:

<!-- 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></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </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: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> null</span><span style="color: #BFC7D5;">],</span></div><div class="line"><span style="color: #82AAFF;"> </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: #82AAFF;"> </span><span style="color: #89DDFF;">=></span><span style="color: #82AAFF;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Bookcase</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;">$filtered</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">whereNull</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: #BEC5D4;">$filtered</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</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;">        ['name' => null],</span></div><div class="line"><span style="color: #697098;">    ]</span></div><div class="line"><span style="color: #697098;">*/</span></div>

wrap()

The static wrap method wraps the given value in a collection when applicable:

<!-- 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;">Collection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><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: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><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: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div><div class="line"> </div><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: #FFCB8B;">Collection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">wrap</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;">John Doe</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">));</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ['John Doe']</span></div>

zip()

The zip method merges together the values of the given array with the values of the
original collection at their corresponding index:

<!-- 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;">Chair</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;">Desk</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">]);</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$zipped</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$collection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">zip</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></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$zipped</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> [['Chair', 100], ['Desk', 200]]</span></div>

Higher Order Messages

Collections also provide support for «higher order messages», which are short-cuts for performing
common actions on collections. The collection methods that provide higher order messages are: average, avg,
contains, each, every,
filter, first, flatMap, groupBy, keyBy, map, max, min, partition, reject, skipUntil, skipWhile, some, sortBy,
sortByDesc, sum, takeUntil, takeWhile, and unique.

Each higher order message can be accessed as a dynamic property on a collection instance. For
instance, let’s use the each higher order message to call a method on each object
within a collection:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">votes</span><span style="color: #D9F5DD;">'</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: #F78C6C;">500</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #89DDFF;">->each-></span><span style="color: #82AAFF;">markAsVip</span><span style="color: #BFC7D5;">();</span></div>

Likewise, we can use the sum higher order message to gather the total number of
«votes» for a collection of users:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">group</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Development</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">get</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: #BEC5D4;">$users</span><span style="color: #89DDFF;">->sum->votes</span><span style="color: #BFC7D5;">;</span></div>

Lazy Collections

Introduction

exclamation

Before learning more about Laravel’s lazy collections, take some time to familiarize
yourself with PHP
generators
.

To supplement the already powerful Collection class, the LazyCollection
class leverages PHP’s generators to
allow you to work with very large datasets while keeping memory usage low.

For example, imagine your application needs to process a multi-gigabyte log file while taking
advantage of Laravel’s collection methods to parse the logs. Instead of reading the entire file
into memory at once, lazy collections may be used to keep only a small part of the file in
memory at a given time:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">LogEntry</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;">LazyCollection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</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: #BEC5D4;">$handle</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fopen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">log.txt</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;">r</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> ((</span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fgets</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">handle</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: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$line</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;">chunk</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">4</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">map</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;">$lines</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;">LogEntry</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">fromLines</span><span style="color: #BFC7D5;">(</span><span style="color: #BEC5D4;">$lines</span><span style="color: #BFC7D5;">);</span></div><div class="line"><span style="color: #BFC7D5;">})</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;">LogEntry</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$logEntry</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;"> Process the log entry...</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

Or, imagine you need to iterate through 10,000 Eloquent models. When using
traditional Laravel collections, all 10,000 Eloquent models must be loaded into
memory at the same time:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</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;">filter</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">500</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">});</span></div>

However, the query builder’s cursor method returns a LazyCollection
instance. This allows you to still only run a single query against the database but
also only keep one Eloquent model loaded in memory at a time. In this example, the
filter callback is not executed until we actually iterate over each user
individually, allowing for a drastic reduction in memory usage:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">filter</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: #C792EA;">return</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">></span><span style="color: #BFC7D5;"> </span><span style="color: #F78C6C;">500</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: #C792EA;">foreach</span><span style="color: #BFC7D5;"> (</span><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">as</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;">echo</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$user</span><span style="color: #89DDFF;">->id</span><span style="color: #BFC7D5;">;</span></div><div class="line"><span style="color: #BFC7D5;">}</span></div>

Creating Lazy
Collections

To create a lazy collection instance, you should pass a PHP generator function to the
collection’s make method:

<!-- 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;">LazyCollection</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">make</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: #BEC5D4;">$handle</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fopen</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">log.txt</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;">r</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">while</span><span style="color: #BFC7D5;"> ((</span><span style="color: #BEC5D4;">$line</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">fgets</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">handle</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: #BFC7D5;"> </span><span style="color: #C792EA;">yield</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$line</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>

The Enumerable Contract

Almost all methods available on the Collection class are also available on the
LazyCollection class. Both of these classes implement the
Illuminate\Support\Enumerable contract, which defines the following methods:

exclamation

Methods that mutate the collection (such as shift, pop,
prepend etc.) are not available on the
LazyCollection class.

Lazy Collection Methods

In addition to the methods defined in the Enumerable contract, the
LazyCollection class contains the following methods:

takeUntilTimeout()

The takeUntilTimeout method returns a new lazy collection that will enumerate values
until the specified time. After that time, the collection will then stop enumerating:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">INF</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntilTimeout</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: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;">$number</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> {</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #82AAFF;">dump</span><span style="color: #BFC7D5;">($</span><span style="color: #BEC5D4;">number</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">sleep</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></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> ...</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 58</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 59</span></div>

To illustrate the usage of this method, imagine an application that submits invoices from the
database using a cursor. You could define a scheduled
task
that runs every 15 minutes and only processes invoices for a maximum of 14 minutes:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> App\Models\</span><span style="color: #FFCB8B;">Invoice</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;">Carbon</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Invoice</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">pending</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">takeUntilTimeout</span><span style="color: #BFC7D5;">(</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Carbon</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">createFromTimestamp</span><span style="color: #BFC7D5;">(</span><span style="color: #82AAFF;">LARAVEL_START</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">add</span><span style="color: #BFC7D5;">(</span><span style="color: #F78C6C;">14</span><span style="color: #BFC7D5;">, </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">minutes</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><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;">Invoice</span><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #D9F5DD;">)</span><span style="color: #BFC7D5;"> => </span><span style="color: #BEC5D4;">$invoice</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">submit</span><span style="color: #BFC7D5;">());</span></div>

tapEach()

While the each method calls the given callback for each item in the collection right
away, the tapEach method only calls the given callback as the items are being
pulled out of the list one by one:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> Nothing has been dumped so far...</span></div><div class="line"><span style="color: #BEC5D4;">$lazyCollection</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">LazyCollection</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">times</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">INF</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">tapEach</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: #82AAFF;">dump</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;"> Three items are dumped...</span></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: #BEC5D4;">$lazyCollection</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">take</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;">all</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 1</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 2</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> 3</span></div>

throttle()

The throttle method will throttle the lazy collection such that each value is
returned after the specified number of seconds. This method is especially useful for situations
where you may be interacting with external APIs that rate limit incoming requests:

<!-- Syntax highlighted by torchlight.dev --><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"> </div><div class="line"><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">where</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">vip</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">, </span><span style="color: #82AAFF;">true</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">throttle</span><span style="color: #BFC7D5;">(seconds: </span><span style="color: #F78C6C;">1</span><span style="color: #BFC7D5;">)</span></div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">each</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;"> Call external API...</span></div><div class="line"><span style="color: #BFC7D5;">    });</span></div>

remember()

The remember method returns a new lazy collection that will remember any values that
have already been enumerated and will not retrieve them again on subsequent collection
enumerations:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> No query has been executed yet...</span></div><div class="line"><span style="color: #BEC5D4;">$users</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">User</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">cursor</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">remember</span><span style="color: #BFC7D5;">();</span></div><div class="line"> </div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The query is executed...</span></div><div class="line"><span style="color: #697098;">//</span><span style="color: #697098;"> The first 5 users are hydrated from the <code>database...
$users->take(5)->all();
 
// First 5 users come from the collection's cache...
// The rest are hydrated from the database...
$users->take(20)->all();



Concurrency

Concurrency

Introduction

exclamation

Laravel’s Concurrency facade is currently in beta while we gather community
feedback.

Sometimes you may need to execute several slow tasks which do not depend on one another. In many
cases, significant performance improvements can be realized by executing the tasks concurrently.
Laravel’s Concurrency facade provides a simple, convenient API for executing
closures concurrently.

Concurrency
Compatibility

If you upgraded to Laravel 11.x from a Laravel 10.x application, you may need to add the
ConcurrencyServiceProvider to the providers array in your
application’s config/app.php configuration file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">providers</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;"> </span><span style="color: #89DDFF;">=></span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">ServiceProvider</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">defaultProviders</span><span style="color: #BFC7D5;">()</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">merge</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: #697098;">     * Package Service Providers...</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line line-add line-has-background" style="background-color: #99b76d23"><span style="color: #C3E88D;">    Illuminate\Concurrency\</span><span style="color: #C3E88D;">ConcurrencyServiceProvider</span><span style="color: #C3E88D;">::</span><span style="color: #C3E88D;">class</span><span style="color: #C3E88D;">, </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: #697098;">     * Application Service Providers...</span></div><div class="line"><span style="color: #697098;"> </span><span style="color: #697098;">*/</span></div><div class="line"><span style="color: #BFC7D5;">    App\Providers\</span><span style="color: #FFCB8B;">AppServiceProvider</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;">    App\Providers\</span><span style="color: #FFCB8B;">AuthServiceProvider</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;"> App\Providers\BroadcastServiceProvider::class,</span></div><div class="line"><span style="color: #BFC7D5;">    App\Providers\</span><span style="color: #FFCB8B;">EventServiceProvider</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;">    App\Providers\</span><span style="color: #FFCB8B;">RouteServiceProvider</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: #89DDFF;">-></span><span style="color: #82AAFF;">toArray</span><span style="color: #BFC7D5;">(),</span></div>

How it Works

Laravel achieves concurrency by serializing the given closures and dispatching them to a hidden
Artisan CLI command, which unserializes the closures and invokes it within its own PHP process.
After the closure has been invoked, the resulting value is serialized back to the parent
process.

The Concurrency facade supports three drivers: process (the default),
fork, and sync.

The fork driver offers improved performance compared to the default
process driver, but it may only be used within PHP’s CLI context, as PHP does not
support forking during web requests. Before using the fork driver, you need to
install the spatie/fork package:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>composer require spatie/fork

The sync driver is primarily useful during testing when you want to disable all
concurrency and simply execute the given closures in sequence within the parent process.

Running Concurrent Tasks

To run concurrent tasks, you may invoke the Concurrency facade’s run
method. The run method accepts an array of closures which should be executed
simultaneously in child PHP processes:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Concurrency</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"> </div><div class="line"><span style="color: #BFC7D5;">[</span><span style="color: #BEC5D4;">$userCount</span><span style="color: #BFC7D5;">, </span><span style="color: #BEC5D4;">$orderCount</span><span style="color: #BFC7D5;">] </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Concurrency</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">run</span><span style="color: #BFC7D5;">([</span></div><div class="line"><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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">count</span><span style="color: #BFC7D5;">(),</span></div><div class="line"><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;">DB</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">table</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: #82AAFF;">count</span><span style="color: #BFC7D5;">(),</span></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>

To use a specific driver, you may use the driver method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BEC5D4;">$results</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FFCB8B;">Concurrency</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">driver</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">fork</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">)</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">run</span><span style="color: #BFC7D5;">(</span><span style="color: #89DDFF;">...</span><span style="color: #BFC7D5;">);</span></div>

Or, to change the default concurrency driver, you should publish the concurrency
configuration file via the config:publish Artisan command and update
the default option within the file:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan config:publish concurrency

Deferring Concurrent
Tasks

If you would like to execute an array of closures concurrently, but are not interested in the
results returned by those closures, you should consider using the defer method.
When the defer method is invoked, the given closures are not executed immediately.
Instead, Laravel will execute the closures concurrently after the HTTP response has been sent to
the user:

<!-- 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\Support\Facades\</span><span style="color: #FFCB8B;">Concurrency</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #FFCB8B;">Concurrency</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">defer</span><span style="color: #BFC7D5;">([</span></div><div class="line"><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;">Metrics</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">report</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">users</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">),</span></div><div class="line"><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;">Metrics</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">report</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></div><div class="line"><span style="color: #BFC7D5;">]);</span></div>


Configuration

Configuration

Introduction

All of the configuration files for the Laravel framework are stored in the
config directory. Each option is documented, so feel free to look through the files
and get familiar with the options available to you.

These configuration files allow you to configure things like your
database connection information, your mail server information, as well as various
other core configuration values such as your application timezone and encryption
key.

The about Command

Laravel can display an overview of your application’s configuration,
drivers, and environment via the about Artisan command.

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan about

If you’re only interested in a particular section of the application overview
output, you may filter for that section using the --only option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan about --only=environment

Or, to explore a specific configuration file’s values in detail, you may use the
config:show Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan config:show database

Environment
Configuration

It is often helpful to have different configuration values based on the
environment where the application is running. For example, you may wish to use a
different cache driver locally than you do on your production server.

To make this a cinch, Laravel utilizes the DotEnv PHP library. In a fresh Laravel
installation, the root directory of your application will contain a .env.example
file that defines many common environment variables. During the Laravel
installation process, this file will automatically be copied to .env.

Laravel’s default .env file contains some common configuration values
that may differ based on whether your application is running locally or on a production web
server. These values are then read by the configuration files within the
config directory using Laravel’s env function.

If you are developing with a team, you may wish to continue including and updating the
.env.example file with your application. By putting placeholder values in the
example configuration file, other developers on your team can clearly see which
environment variables are needed to run your application.

lightbulb

Any variable in your .env file can be overridden by external
environment variables such as server-level or system-level
environment variables.

Environment File
Security

Your .env file should not be committed to your application’s source control, since
each developer / server using your application could require a different
environment configuration. Furthermore, this would be a security risk
in the event an intruder gains access to your source control repository, since any sensitive
credentials would get exposed.

However, it is possible to encrypt your environment file using Laravel’s built-in environment encryption. Encrypted
environment files may be placed in source control safely.

Additional Environment
Files

Before loading your application’s environment variables, Laravel determines if an
APP_ENV environment variable has been externally provided or if the
--env CLI argument has been specified. If so, Laravel will attempt to load an
.env.[APP_ENV] file if it exists. If it does not exist, the default
.env file will be loaded.

Environment Variable
Types

All variables in your .env files are typically parsed as strings, so some reserved
values have been created to allow you to return a wider range of types from the
env() function:

.env Value env() Value
true (bool) true
(true) (bool) true
false (bool) false
(false) (bool) false
empty (string) »
(empty) (string) »
null (null) null
(null) (null) null

If you need to define an environment variable with a value that contains spaces, you
may do so by enclosing the value in double quotes:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">APP_NAME</span><span style="color: #BFC7D5;">=</span><span style="color: #D9F5DD;">"</span><span style="color: #C3E88D;">My Application</span><span style="color: #D9F5DD;">"</span></div>

Retrieving Environment Configuration

All of the variables listed in the .env file will be loaded into the
$_ENV PHP super-global when your application receives a request. However, you may
use the env function to retrieve values from these variables in your
configuration files. In fact, if you review the Laravel
configuration files, you will notice many of the options are already using this
function:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">debug</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;"><code>env('APP_DEBUG', false),

The second value passed to the env function is the «default value». This value will
be returned if no environment variable exists for the given key.

Determining the Current Environment

The current application environment is determined via the APP_ENV
variable from your .env file. You may access this value via the
environment method on the App facade:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">App</span><span style="color: #BFC7D5;">;</span></div><div class="line"> </div><div class="line"><span style="color: #BEC5D4;">$<code>environment = App::environment();

You may also pass arguments to the environment method to determine if the
environment matches a given value. The method will return true if the
environment matches any of the given values:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">if</span><span style="color: #BFC7D5;"> (</span><span style="color: #FFCB8B;">App</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;"><code>environment('local')) {
// The environment is local
}
 
if (App::environment(['local', 'staging'])) {
// The environment is either local OR staging...
}
lightbulb

The current application environment detection can be overridden by defining a
server-level APP_ENV environment variable.

Encrypting Environment
Files

Unencrypted environment files should never be stored in source control. However,
Laravel allows you to encrypt your environment files so that they may safely be
added to source control with the rest of your application.

Encryption

To encrypt an environment file, you may use the env:encrypt command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:encrypt

Running the env:encrypt command will encrypt your .env file and place
the encrypted contents in an .env.encrypted file. The decryption key is presented
in the output of the command and should be stored in a secure password manager. If you would
like to provide your own encryption key you may use the --key option when invoking
the command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:encrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

lightbulb

The length of the key provided should match the key length required by the encryption cipher
being used. By default, Laravel will use the AES-256-CBC cipher which requires
a 32 character key. You are free to use any cipher supported by Laravel’s encrypter by passing the --cipher option when
invoking the command.

If your application has multiple environment files, such as .env and
.env.staging, you may specify the environment file that should be
encrypted by providing the environment name via the --env option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:encrypt --env=staging

Decryption

To decrypt an environment file, you may use the env:decrypt command.
This command requires a decryption key, which Laravel will retrieve from the
LARAVEL_ENV_ENCRYPTION_KEY environment variable:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:decrypt

Or, the key may be provided directly to the command via the --key option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:decrypt --key=3UVsEgGVK36XN82KKeyLFMhvosbZN1aF

When the env:decrypt command is invoked, Laravel will decrypt the contents of the
.env.encrypted file and place the decrypted contents in the .env file.

The --cipher option may be provided to the env:decrypt command in order
to use a custom encryption cipher:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:decrypt --key=qUWuNRdfuImXcKxZ --cipher=AES-128-CBC

If your application has multiple environment files, such as .env and
.env.staging, you may specify the environment file that should be
decrypted by providing the environment name via the --env option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:decrypt --env=staging

In order to overwrite an existing environment file, you may provide the
--force option to the env:decrypt command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan env:decrypt --force

Accessing
Configuration Values

You may easily access your configuration values using the Config facade
or global config function from anywhere in your application. The
configuration values may be accessed using «dot» syntax, which includes the name of
the file and option you wish to access. A default value may also be specified and will be
returned if the configuration option does not exist:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #C792EA;">use</span><span style="color: #BFC7D5;"> Illuminate\Support\Facades\</span><span style="color: #FFCB8B;">Config</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;">Config</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;">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;"><code>config('app.timezone');
 
// Retrieve a default value if the configuration value does not exist...
$value = config('app.timezone', 'Asia/Seoul');

To set configuration values at runtime, you may invoke the Config
facade’s set method or pass an array to the config function:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Config</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">set</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: #D9F5DD;">'</span><span style="color: #C3E88D;">America/Chicago</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #82AAFF;"><code>config(['app.timezone' => 'America/Chicago']);

To assist with static analysis, the Config facade also provides typed
configuration retrieval methods. If the retrieved configuration value
does not match the expected type, an exception will be thrown:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Config</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">string</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"><code>config-key');
Config::integer('config-key');
Config::float('config-key');
Config::boolean('config-key');
Config::array('config-key');

Configuration Caching

To give your application a speed boost, you should cache all of your configuration
files into a single file using the config:cache Artisan command. This will combine
all of the configuration options for your application into a single file which can
be quickly loaded by the framework.

You should typically run the php artisan config:cache command as part of your
production deployment process. The command should not be run during local development as
configuration options will frequently need to be changed during the course of your
application’s development.

Once the configuration has been cached, your application’s .env file
will not be loaded by the framework during requests or Artisan commands; therefore, the
env function will only return external, system level environment
variables.

For this reason, you should ensure you are only calling the env function from within
your application’s configuration (config) files. You can see many
examples of this by examining Laravel’s default configuration files. Configuration
values may be accessed from anywhere in your application using the config function
described above.

The config:clear command may be used to purge the cached configuration:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan config:clear

exclamation

If you execute the config:cache command during your deployment process, you
should be sure that you are only calling the env function from within your
configuration files. Once the configuration has been cached, the
.env file will not be loaded; therefore, the env function will
only return external, system level environment variables.

Configuration Publishing

Most of Laravel’s configuration files are already published in your application’s
config directory; however, certain configuration files like
cors.php and view.php are not published by default, as most
applications will never need to modify them.

However, you may use the config:publish Artisan command to publish any
configuration files that are not published by default:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan config:publish
 
php artisan config:publish --all

Debug Mode

The debug option in your config/app.php configuration file
determines how much information about an error is actually displayed to the user. By default,
this option is set to respect the value of the APP_DEBUG environment
variable, which is stored in your .env file.

exclamation

For local development, you should set the APP_DEBUG environment
variable to true. In your production environment, this
value should always be false. If the variable is set to true
in production, you risk exposing sensitive configuration values to your
application’s end users.

Maintenance Mode

When your application is in maintenance mode, a custom view will be displayed for
all requests into your application. This makes it easy to «disable» your application while it is
updating or when you are performing maintenance. A maintenance mode check is included in the
default middleware stack for your application. If the application is in maintenance
mode, a Symfony\Component\HttpKernel\Exception\HttpException instance will be
thrown with a status code of 503.

To enable maintenance mode, execute the down Artisan command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down

If you would like the Refresh HTTP header to be sent with all maintenance mode
responses, you may provide the refresh option when invoking the down
command. The Refresh header will instruct the browser to automatically refresh the
page after the specified number of seconds:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --refresh=15

You may also provide a retry option to the down command, which will be
set as the Retry-After HTTP header’s value, although browsers generally ignore this
header:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --retry=60

Bypassing Maintenance
Mode

To allow maintenance mode to be bypassed using a secret token, you may use the
secret option to specify a maintenance mode bypass token:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

After placing the application in maintenance mode, you may navigate to the application URL
matching this token and Laravel will issue a maintenance mode bypass cookie to your browser:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;">https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515</span></div>

If you would like Laravel to generate the secret token for you, you may use the
with-secret option. The secret will be displayed to you once the application is in
maintenance mode:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --with-secret

When accessing this hidden route, you will then be redirected to the /
route of the application. Once the cookie has been issued to your browser, you will
be able to browse the application normally as if it was not in maintenance mode.

lightbulb

Your maintenance mode secret should typically consist of alpha-numeric characters and,
optionally, dashes. You should avoid using characters that have special meaning in URLs such
as ? or &.

Maintenance Mode on Multiple Servers

By default, Laravel determines if your application is in maintenance mode using a file-based
system. This means to activate maintenance mode, the php artisan down command has
to be executed on each server hosting your application.

Alternatively, Laravel offers a cache-based method for handling maintenance mode. This method
requires running the php artisan down command on just one server. To use this
approach, modify the «driver» setting in the config/app.php file of your
application to cache. Then, select a cache store that is accessible by
all your servers. This ensures the maintenance mode status is consistently maintained across
every server:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">maintenance</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;">driver</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;">cache</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;">store</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;"><code>database',
],

Pre-Rendering the Maintenance Mode View

If you utilize the php artisan down command during deployment, your users may still
occasionally encounter errors if they access the application while your Composer dependencies or
other infrastructure components are updating. This occurs because a significant part of the
Laravel framework must boot in order to determine your application is in maintenance mode and
render the maintenance mode view using the templating engine.

For this reason, Laravel allows you to pre-render a maintenance mode view that will
be returned at the very beginning of the request cycle. This view is rendered
before any of your application’s dependencies have loaded. You may pre-render a template of your
choice using the down command’s render option:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --render="errors::503"

Redirecting Maintenance Mode Requests

While in maintenance mode, Laravel will display the maintenance mode view for all
application URLs the user attempts to access. If you wish, you may instruct Laravel to redirect
all requests to a specific URL. This may be accomplished using the redirect option.
For example, you may wish to redirect all requests to the / URI:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan down --redirect=/

Disabling Maintenance
Mode

To disable maintenance mode, use the up command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #BFC7D5;"><code>php artisan up

lightbulb

You may customize the default maintenance mode template by defining your own template at
resources/views/errors/503.blade.php.

Maintenance Mode and Queues

While your application is in maintenance mode, no queued jobs will be
handled. The jobs will continue to be handled as normal once the application is out of
maintenance mode.

Alternatives
to Maintenance Mode

Since maintenance mode requires your application to have several seconds of downtime, consider
alternatives like Laravel Vapor and Envoyer to accomplish zero-downtime deployment with Laravel.


Console Tests

Console Tests

Introduction

In addition to simplifying HTTP testing, Laravel provides a simple API for testing your
application’s custom console commands.

Success / Failure
Expectations

To get started, let’s explore how to make assertions regarding an Artisan command’s exit code. To
accomplish this, we will use the artisan method to invoke an Artisan command from
our test. Then, we will use the assertExitCode method to assert that the command
completed with a given exit code:


<!-- 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;">console command</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;"><code>artisan('inspire')->assertExitCode(0);
});

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Test a console command.</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;">test_console_command</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;"><code>artisan('inspire')->assertExitCode(0);
}

You may use the assertNotExitCode method to assert that the command did not exit
with a given exit code:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>artisan('inspire')->assertNotExitCode(1);

Of course, all terminal commands typically exit with a status code of 0 when they
are successful and a non-zero exit code when they are not successful. Therefore, for
convenience, you may utilize the assertSuccessful and assertFailed
assertions to assert that a given command exited with a successful exit code or not:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>artisan('inspire')->assertSuccessful();
 
$this->artisan('inspire')->assertFailed();

Input / Output
Expectations

Laravel allows you to easily «mock» user input for your console commands using the
expectsQuestion method. In addition, you may specify the exit code and text that
you expect to be output by the console command using the assertExitCode and
expectsOutput methods. For example, consider the following console command:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FFCB8B;">Artisan</span><span style="color: #89DDFF;">::</span><span style="color: #82AAFF;">command</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">question</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: #BEC5D4;">$name</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">ask</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">What is your name?</span><span style="color: #D9F5DD;">'</span><span style="color: #BFC7D5;">);</span></div><div class="line"> </div><div class="line"><span style="color: #BFC7D5;"> </span><span style="color: #BEC5D4;">$language</span><span style="color: #BFC7D5;"> </span><span style="color: #C792EA;">=</span><span style="color: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">choice</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Which language do you prefer?</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></div><div class="line"><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"><span style="color: #BFC7D5;"> </span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Python</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: #BFC7D5;"> </span><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;">line</span><span style="color: #BFC7D5;">(</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;">Your name is </span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$name</span><span style="color: #89DDFF;">.</span><span style="color: #D9F5DD;">'</span><span style="color: #C3E88D;"> and you prefer </span><span style="color: #D9F5DD;">'</span><span style="color: #89DDFF;">.</span><span style="color: #BEC5D4;">$language</span><span style="color: #89DDFF;">.</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: #BFC7D5;">});</span></div>

You may test this command with the following test:


<!-- 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;">console command</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;"><code>artisan('question')
->expectsQuestion('What is your name?', 'Taylor Otwell')
->expectsQuestion('Which language do you prefer?', 'PHP')
->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
->assertExitCode(0);
});

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Test a console command.</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;">test_console_command</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;"><code>artisan('question')
->expectsQuestion('What is your name?', 'Taylor Otwell')
->expectsQuestion('Which language do you prefer?', 'PHP')
->expectsOutput('Your name is Taylor Otwell and you prefer PHP.')
->doesntExpectOutput('Your name is Taylor Otwell and you prefer Ruby.')
->assertExitCode(0);
}

If you are utilizing the search or multisearch functions provided by Laravel Prompts, you may use the expectsSearch
assertion to mock the user’s input, search results, and selection:


<!-- 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;">console command</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;"><code>artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
});

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Test a console command.</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;">test_console_command</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;"><code>artisan('example')
->expectsSearch('What is your name?', search: 'Tay', answers: [
'Taylor Otwell',
'Taylor Swift',
'Darian Taylor'
], answer: 'Taylor Otwell')
->assertExitCode(0);
}

You may also assert that a console command does not generate any output using the
doesntExpectOutput method:


<!-- 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;">console command</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;"><code>artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
});

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Test a console command.</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;">test_console_command</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;"><code>artisan('example')
->doesntExpectOutput()
->assertExitCode(0);
}

The expectsOutputToContain and doesntExpectOutputToContain methods may
be used to make assertions against a portion of the output:


<!-- 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;">console command</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;"><code>artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
});

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #697098;">/**</span></div><div class="line"><span style="color: #697098;"> * Test a console command.</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;">test_console_command</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;"><code>artisan('example')
->expectsOutputToContain('Taylor')
->assertExitCode(0);
}

Confirmation
Expectations

When writing a command which expects confirmation in the form of a «yes» or «no» answer, you may
utilize the expectsConfirmation method:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>artisan('module:import')
->expectsConfirmation('Do you really wish to run this command?', 'no')
->assertExitCode(1);

Table Expectations

If your command displays a table of information using Artisan’s table method, it can
be cumbersome to write output expectations for the entire table. Instead, you may use the
expectsTable method. This method accepts the table’s headers as its first argument
and the table’s data as its second argument:

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #FF5572;">$this</span><span style="color: #89DDFF;">-></span><span style="color: #82AAFF;"><code>artisan('users:all')
->expectsTable([
'ID',
'Email',
], [
]);

Console Events

By default, the Illuminate\Console\Events\CommandStarting and
Illuminate\Console\Events\CommandFinished events are not dispatched while running
your application’s tests. However, you can enable these events for a given test class by adding
the Illuminate\Foundation\Testing\WithConsoleEvents trait to the class:


<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
use Illuminate\Foundation\Testing\WithConsoleEvents;
 
uses(WithConsoleEvents::class);
 
// ...

<!-- Syntax highlighted by torchlight.dev --><div class="line"><span style="color: #D3423E;"><code>php</span
 
namespace Tests\Feature;
 
use Illuminate\Foundation\Testing\WithConsoleEvents;
use Tests\TestCase;
 
class ConsoleEventTest extends TestCase
{
use WithConsoleEvents;
 
// ...
}




468

Enviar comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *