Skip to content
Open
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
5 changes: 5 additions & 0 deletions packages/core/src/Base/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function __construct(array $attributes = [])
}
}

public function setAttribute($key, $value): mixed
{
return parent::setAttribute($key, is_string($value) ? trim($value) : $value);
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/core/Unit/Base/BaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lunar\Base\BaseModel;
use Lunar\Base\Traits\HasModelExtending;
use Lunar\Models\Collection as ModelsCollection;
use Lunar\Models\CustomerGroup;
use Lunar\Models\Product;
use Lunar\Models\Url;
use Lunar\Tests\Core\TestCase;
Expand Down Expand Up @@ -69,3 +70,11 @@
$uses = class_uses_recursive(BaseModel::class);
expect(in_array(HasModelExtending::class, $uses))->toBeTrue();
});

test('trims leading and trailing whitespace from string attributes', function () {
$group = CustomerGroup::factory()->create([
'handle' => ' my-group ',
]);

expect($group->handle)->toBe('my-group');
});