Skip to content
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

Add option to treat Soft Deletes field as string or not #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
// 'soft_deletes' => [
// 'enabled' => true,
// 'field' => 'deleted_at',
// 'field_as_string' => true,
// ],

/*
Expand Down
22 changes: 20 additions & 2 deletions src/Coders/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class Model
*/
protected $DELETED_AT;

/**
* @var bool
*/
protected $softDeletesFieldAsString = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -193,6 +198,7 @@ protected function configure()
// Soft deletes settings
$this->withSoftDeletes($this->config('soft_deletes.enabled', $this->config('soft_deletes', false)));
$this->withDeletedAtField($this->config('soft_deletes.field', $this->getDefaultDeletedAtField()));
$this->withSoftDeletesFieldAsString($this->config('soft_deletes.field_as_string', true));

// Connection settings
$this->withConnection($this->config('connection', false));
Expand Down Expand Up @@ -255,8 +261,8 @@ protected function parseColumn(Fluent $column)
$propertyName = $this->usesPropertyConstants() ? 'self::'.strtoupper($column->name) : $column->name;

// Due to some casting problems when converting null to a Carbon instance,
// we are going to treat Soft Deletes field as string.
if ($column->name == $this->getDeletedAtField()) {
// by default, we are going to treat Soft Deletes field as string.
if ($column->name == $this->getDeletedAtField() && $this->softDeletesFieldAsString) {
$cast = 'string';
}

Expand Down Expand Up @@ -657,6 +663,18 @@ public function withDeletedAtField($field)
return $this;
}

/**
* @param string $field
*
* @return $this
*/
public function withSoftDeletesFieldAsString($softDeletesFieldAsString)
{
$this->softDeletesFieldAsString = $softDeletesFieldAsString;

return $this;
}

/**
* @return string
*/
Expand Down