engelsystem/src/Controllers/CleanupModel.php

39 lines
1.1 KiB
PHP
Raw Normal View History

2020-10-21 00:11:33 +02:00
<?php
namespace Engelsystem\Controllers;
use ArrayAccess;
use Illuminate\Database\Eloquent\Model;
trait CleanupModel
{
/**
* Used to replace null values with en empty string
*
* Required because isset on a null value returns false which gets interpreted as missing properties by Twig
*
* @param Model[]|ArrayAccess|Model $models
* @param string[] $attributes
*/
protected function cleanupModelNullValues($models, array $attributes = [])
{
2020-11-21 20:54:04 +01:00
if (!$models) {
return;
}
2020-10-21 00:11:33 +02:00
$models = $models instanceof Model ? [$models] : $models;
foreach ($models as $model) {
/** @var Model $model */
$attributes = $attributes ?: array_merge(
array_keys($model->getAttributes()),
array_keys($model->getCasts()),
$model->getFillable(),
$model->getDates()
);
foreach ($attributes as $attribute) {
$model->$attribute = is_null($model->$attribute) ? '' : $model->$attribute;
}
}
}
}