Skip to content

Commit 6e98daa

Browse files
author
Smef
authored
Merge pull request #30
Use Model's default setAttribute and then process specific date logic
2 parents 337fea4 + 440e917 commit 6e98daa

File tree

1 file changed

+8
-40
lines changed

1 file changed

+8
-40
lines changed

src/Database/Eloquent/FMModel.php

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Database\Eloquent\Relations\Pivot;
1313
use Illuminate\Http\File;
1414
use Illuminate\Http\UploadedFile;
15+
use Illuminate\Support\Arr;
1516
use Illuminate\Support\Collection;
1617
use Illuminate\Support\Str;
1718

@@ -471,55 +472,22 @@ public function getTable()
471472
*/
472473
public function setAttribute($key, $value)
473474
{
474-
// First we will check for the presence of a mutator for the set operation
475-
// which simply lets the developers tweak the attribute as it is set on
476-
// the model, such as "json_encoding" an listing of data for storage.
477-
if ($this->hasSetMutator($key)) {
478-
return $this->setMutatedAttributeValue($key, $value);
479-
}
480-
481-
// If an attribute is listed as a "date", we'll convert it from a DateTime
482-
// instance into a form proper for storage on the database tables using
483-
// the connection grammar's date format. We will auto set the values.
484-
elseif ($value && $this->isDateAttribute($key)) {
485-
$value = $this->fromDateTime($value);
486-
487-
// When writing dates the regular datetime format won't work, so we have to get JUST the date value
488-
489-
if ($this->casts[$key] === 'date') {
490-
$exploded = explode(' ', $value);
491-
$value = $exploded[0];
492-
}
493-
}
494-
495-
if ($this->isClassCastable($key)) {
496-
$this->setClassCastableAttribute($key, $value);
475+
parent::setAttribute($key, $value);
497476

498-
return $this;
499-
}
500-
501-
if (!is_null($value) && $this->isJsonCastable($key)) {
502-
$value = $this->castAttributeAsJson($key, $value);
503-
}
504-
505-
// If this attribute contains a JSON ->, we'll set the proper value in the
506-
// attribute's underlying array. This takes care of properly nesting an
507-
// attribute in the array's value in the case of deeply nested items.
508-
if (Str::contains($key, '->')) {
509-
return $this->fillJsonAttribute($key, $value);
510-
}
477+
$value = $this->attributes[$key];
511478

512-
if (!is_null($value) && $this->isEncryptedCastable($key)) {
513-
$value = $this->castAttributeAsEncryptedString($key, $value);
479+
// When writing dates the regular datetime format won't work, so we have to get JUST the date value
480+
if ($this->isDateAttribute($key) && $this->hasCast($key, ['date'])) {
481+
$value = Arr::first(explode(' ', $value));
514482
}
515483

516484
// FileMaker can't handle true and false, so we need to change to 1 and 0
517-
if (is_bool($value)){
485+
if (is_bool($value)) {
518486
$value = $value ? 1 : 0;
519487
}
520488

521489
// FileMaker can't handle null, so change it to ''
522-
if (is_null($value)){
490+
if (is_null($value)) {
523491
$value = '';
524492
}
525493

0 commit comments

Comments
 (0)