Skip to content

Commit a22a2e4

Browse files
committed
Adds a test for calling the callback events and mention it in the docs
1 parent 86b8da1 commit a22a2e4

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class User extends Model
176176

177177
## Transforming Models Between Types
178178

179-
You can transform a model from one child type to another using the `become()` method.
179+
You may transform a model from one type to another using the `become()` method.
180180

181181
```php
182182
namespace App\Models;
@@ -220,11 +220,11 @@ class ShippedOrder extends Order
220220
```
221221

222222
```php
223-
use App\Models\PendingOrder;
223+
use App\Models\Order;
224224
use App\Models\ShippedOrder;
225225

226226
// Retrieve a pending order
227-
$order = PendingOrder::first();
227+
$order = Order::first();
228228

229229
// Ship the order by transforming it
230230
$order = $order->become(ShippedOrder::class);
@@ -235,7 +235,9 @@ $order->save();
235235

236236
### What problem did we just solve?
237237

238-
The `become()` method allows you to convert a model from one child type to another without losing any data. This is useful when you need to change the state or type of an existing record, such as transitioning an order from pending to shipped, or converting a draft post to a published post.
238+
The `become()` method will return a new instance of the specified child model with all the attributes of the original model. You must call `save()` on the returned model to persist the change to the database. This allows you to easily transition a model between different types while maintaining its data integrity, such as changing an order from pending to shipped, or a draft post to a published post.
239+
240+
This is also useful when you're using observers or callbacks, since the specific child model's behavior will be triggered after the transition.
239241

240242
## Laravel Nova Support
241243

tests/Features/ModelsCanBecomeOtherTypesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,27 @@ public function become_sets_the_correct_type_alias()
8080

8181
$this->assertEquals('mallet', $mallet->type);
8282
}
83+
84+
/** @test */
85+
public function come_calls_model_events_on_specified_model(): void
86+
{
87+
$carEventCalled = false;
88+
89+
Car::saved(function () use (&$carEventCalled) {
90+
$carEventCalled = true;
91+
});
92+
93+
$vehicle = Vehicle::create(['type' => 'truck']);
94+
95+
$this->assertFalse($carEventCalled);
96+
97+
$car = $vehicle->become(Car::class);
98+
$car->save();
99+
100+
$this->assertInstanceOf(Car::class, $car);
101+
$this->assertEquals('car', $car->type);
102+
$this->assertEquals($vehicle->id, $car->id);
103+
104+
$this->assertTrue($carEventCalled);
105+
}
83106
}

tests/Models/Vehicle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Vehicle extends Model
99
{
1010
use HasChildren;
1111

12+
public $boot_count = 0;
13+
1214
protected $fillable = [
1315
'type', 'driver_id',
1416
];

0 commit comments

Comments
 (0)