Skip to content

[11.x] remove arbitrary chaining alignment #10166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions billing.md
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@ The amount of time a customer has to pay their invoice before their subscription
If you would like to set a specific [quantity](https://stripe.com/docs/billing/subscriptions/quantities) for the price when creating the subscription, you should invoke the `quantity` method on the subscription builder before creating the subscription:

$user->newSubscription('default', 'price_monthly')
->quantity(5)
->create($paymentMethod);
->quantity(5)
->create($paymentMethod);

<a name="additional-details"></a>
#### Additional Details
Expand All @@ -862,14 +862,14 @@ If you would like to specify additional [customer](https://stripe.com/docs/api/c
If you would like to apply a coupon when creating the subscription, you may use the `withCoupon` method:

$user->newSubscription('default', 'price_monthly')
->withCoupon('code')
->create($paymentMethod);
->withCoupon('code')
->create($paymentMethod);

Or, if you would like to apply a [Stripe promotion code](https://stripe.com/docs/billing/subscriptions/discounts/codes), you may use the `withPromotionCode` method:

$user->newSubscription('default', 'price_monthly')
->withPromotionCode('promo_code_id')
->create($paymentMethod);
->withPromotionCode('promo_code_id')
->create($paymentMethod);

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:

Expand Down Expand Up @@ -1104,8 +1104,8 @@ If the customer is on trial, the trial period will be maintained. Additionally,
If you would like to swap prices and cancel any trial period the customer is currently on, you may invoke the `skipTrial` method:

$user->subscription('default')
->skipTrial()
->swap('price_yearly');
->skipTrial()
->swap('price_yearly');

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:

Expand Down Expand Up @@ -1237,8 +1237,8 @@ If you want to swap a single price on a subscription, you may do so using the `s
$user = User::find(1);

$user->subscription('default')
->findItemOrFail('price_basic')
->swap('price_pro');
->findItemOrFail('price_basic')
->swap('price_pro');

<a name="proration"></a>
#### Proration
Expand Down Expand Up @@ -1329,9 +1329,9 @@ To start using usage billing, you will first need to create a new product in you
You may also start a metered subscription via [Stripe Checkout](#checkout):

$checkout = Auth::user()
->newSubscription('default', [])
->meteredPrice('price_metered')
->checkout();
->newSubscription('default', [])
->meteredPrice('price_metered')
->checkout();

return view('your-checkout-view', [
'checkout' => $checkout,
Expand Down Expand Up @@ -1441,8 +1441,8 @@ By default, the billing cycle anchor is the date the subscription was created or
$anchor = Carbon::parse('first day of next month');

$request->user()->newSubscription('default', 'price_monthly')
->anchorBillingCycleOn($anchor->startOfDay())
->create($request->paymentMethodId);
->anchorBillingCycleOn($anchor->startOfDay())
->create($request->paymentMethodId);

// ...
});
Expand Down Expand Up @@ -1507,8 +1507,8 @@ If you would like to offer trial periods to your customers while still collectin

Route::post('/user/subscribe', function (Request $request) {
$request->user()->newSubscription('default', 'price_monthly')
->trialDays(10)
->create($request->paymentMethodId);
->trialDays(10)
->create($request->paymentMethodId);

// ...
});
Expand All @@ -1523,8 +1523,8 @@ The `trialUntil` method allows you to provide a `DateTime` instance that specifi
use Carbon\Carbon;

$user->newSubscription('default', 'price_monthly')
->trialUntil(Carbon::now()->addDays(10))
->create($paymentMethod);
->trialUntil(Carbon::now()->addDays(10))
->create($paymentMethod);

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:

Expand Down Expand Up @@ -2131,7 +2131,7 @@ First, you could redirect your customer to the dedicated payment confirmation pa

try {
$subscription = $user->newSubscription('default', 'price_monthly')
->create($paymentMethod);
->create($paymentMethod);
} catch (IncompletePayment $exception) {
return redirect()->route(
'cashier.payment',
Expand Down
170 changes: 85 additions & 85 deletions eloquent-factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ Sometimes you may wish to alternate the value of a given model attribute for eac
use Illuminate\Database\Eloquent\Factories\Sequence;

$users = User::factory()
->count(10)
->state(new Sequence(
['admin' => 'Y'],
['admin' => 'N'],
))
->create();
->count(10)
->state(new Sequence(
['admin' => 'Y'],
['admin' => 'N'],
))
->create();

In this example, five users will be created with an `admin` value of `Y` and five users will be created with an `admin` value of `N`.

Expand All @@ -281,28 +281,28 @@ If necessary, you may include a closure as a sequence value. The closure will be
use Illuminate\Database\Eloquent\Factories\Sequence;

$users = User::factory()
->count(10)
->state(new Sequence(
fn (Sequence $sequence) => ['role' => UserRoles::all()->random()],
))
->create();
->count(10)
->state(new Sequence(
fn (Sequence $sequence) => ['role' => UserRoles::all()->random()],
))
->create();

Within a sequence closure, you may access the `$index` or `$count` properties on the sequence instance that is injected into the closure. The `$index` property contains the number of iterations through the sequence that have occurred thus far, while the `$count` property contains the total number of times the sequence will be invoked:

$users = User::factory()
->count(10)
->sequence(fn (Sequence $sequence) => ['name' => 'Name '.$sequence->index])
->create();
->count(10)
->sequence(fn (Sequence $sequence) => ['name' => 'Name '.$sequence->index])
->create();

For convenience, sequences may also be applied using the `sequence` method, which simply invokes the `state` method internally. The `sequence` method accepts a closure or arrays of sequenced attributes:

$users = User::factory()
->count(2)
->sequence(
['name' => 'First User'],
['name' => 'Second User'],
)
->create();
->count(2)
->sequence(
['name' => 'First User'],
['name' => 'Second User'],
)
->create();

<a name="factory-relationships"></a>
## Factory Relationships
Expand All @@ -316,51 +316,51 @@ Next, let's explore building Eloquent model relationships using Laravel's fluent
use App\Models\User;

$user = User::factory()
->has(Post::factory()->count(3))
->create();
->has(Post::factory()->count(3))
->create();

By convention, when passing a `Post` model to the `has` method, Laravel will assume that the `User` model must have a `posts` method that defines the relationship. If necessary, you may explicitly specify the name of the relationship that you would like to manipulate:

$user = User::factory()
->has(Post::factory()->count(3), 'posts')
->create();
->has(Post::factory()->count(3), 'posts')
->create();

Of course, you may perform state manipulations on the related models. In addition, you may pass a closure based state transformation if your state change requires access to the parent model:

$user = User::factory()
->has(
Post::factory()
->count(3)
->state(function (array $attributes, User $user) {
return ['user_type' => $user->type];
})
)
->create();
->has(
Post::factory()
->count(3)
->state(function (array $attributes, User $user) {
return ['user_type' => $user->type];
})
)
->create();

<a name="has-many-relationships-using-magic-methods"></a>
#### Using Magic Methods

For convenience, you may use Laravel's magic factory relationship methods to build relationships. For example, the following example will use convention to determine that the related models should be created via a `posts` relationship method on the `User` model:

$user = User::factory()
->hasPosts(3)
->create();
->hasPosts(3)
->create();

When using magic methods to create factory relationships, you may pass an array of attributes to override on the related models:

$user = User::factory()
->hasPosts(3, [
'published' => false,
])
->create();
->hasPosts(3, [
'published' => false,
])
->create();

You may provide a closure based state transformation if your state change requires access to the parent model:

$user = User::factory()
->hasPosts(3, function (array $attributes, User $user) {
return ['user_type' => $user->type];
})
->create();
->hasPosts(3, function (array $attributes, User $user) {
return ['user_type' => $user->type];
})
->create();

<a name="belongs-to-relationships"></a>
### Belongs To Relationships
Expand All @@ -371,32 +371,32 @@ Now that we have explored how to build "has many" relationships using factories,
use App\Models\User;

$posts = Post::factory()
->count(3)
->for(User::factory()->state([
'name' => 'Jessica Archer',
]))
->create();
->count(3)
->for(User::factory()->state([
'name' => 'Jessica Archer',
]))
->create();

If you already have a parent model instance that should be associated with the models you are creating, you may pass the model instance to the `for` method:

$user = User::factory()->create();

$posts = Post::factory()
->count(3)
->for($user)
->create();
->count(3)
->for($user)
->create();

<a name="belongs-to-relationships-using-magic-methods"></a>
#### Using Magic Methods

For convenience, you may use Laravel's magic factory relationship methods to define "belongs to" relationships. For example, the following example will use convention to determine that the three posts should belong to the `user` relationship on the `Post` model:

$posts = Post::factory()
->count(3)
->forUser([
'name' => 'Jessica Archer',
])
->create();
->count(3)
->forUser([
'name' => 'Jessica Archer',
])
->create();

<a name="many-to-many-relationships"></a>
### Many to Many Relationships
Expand All @@ -407,8 +407,8 @@ Like [has many relationships](#has-many-relationships), "many to many" relations
use App\Models\User;

$user = User::factory()
->has(Role::factory()->count(3))
->create();
->has(Role::factory()->count(3))
->create();

<a name="pivot-table-attributes"></a>
#### Pivot Table Attributes
Expand All @@ -419,44 +419,44 @@ If you need to define attributes that should be set on the pivot / intermediate
use App\Models\User;

$user = User::factory()
->hasAttached(
Role::factory()->count(3),
['active' => true]
)
->create();
->hasAttached(
Role::factory()->count(3),
['active' => true]
)
->create();

You may provide a closure based state transformation if your state change requires access to the related model:

$user = User::factory()
->hasAttached(
Role::factory()
->count(3)
->state(function (array $attributes, User $user) {
return ['name' => $user->name.' Role'];
}),
['active' => true]
)
->create();
->hasAttached(
Role::factory()
->count(3)
->state(function (array $attributes, User $user) {
return ['name' => $user->name.' Role'];
}),
['active' => true]
)
->create();

If you already have model instances that you would like to be attached to the models you are creating, you may pass the model instances to the `hasAttached` method. In this example, the same three roles will be attached to all three users:

$roles = Role::factory()->count(3)->create();

$user = User::factory()
->count(3)
->hasAttached($roles, ['active' => true])
->create();
->count(3)
->hasAttached($roles, ['active' => true])
->create();

<a name="many-to-many-relationships-using-magic-methods"></a>
#### Using Magic Methods

For convenience, you may use Laravel's magic factory relationship methods to define many to many relationships. For example, the following example will use convention to determine that the related models should be created via a `roles` relationship method on the `User` model:

$user = User::factory()
->hasRoles(1, [
'name' => 'Editor'
])
->create();
->hasRoles(1, [
'name' => 'Editor'
])
->create();

<a name="polymorphic-relationships"></a>
### Polymorphic Relationships
Expand Down Expand Up @@ -485,17 +485,17 @@ Polymorphic "many to many" (`morphToMany` / `morphedByMany`) relationships may b
use App\Models\Video;

$videos = Video::factory()
->hasAttached(
Tag::factory()->count(3),
['public' => true]
)
->create();
->hasAttached(
Tag::factory()->count(3),
['public' => true]
)
->create();

Of course, the magic `has` method may also be used to create polymorphic "many to many" relationships:

$videos = Video::factory()
->hasTags(3, ['public' => true])
->create();
->hasTags(3, ['public' => true])
->create();

<a name="defining-relationships-within-factories"></a>
### Defining Relationships Within Factories
Expand Down
Loading