<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingTrait;
class ReportTemplate extends Model
{
use HasFactory;
use SoftDeletes;
use ValidatingTrait;
protected $casts = [
'options' => 'array',
];
protected $fillable = [
'created_by',
'name',
'options',
];
protected $rules = [
'name' => [
'required',
'string',
],
'options' => [
'required',
'array',
],
];
protected static function booted()
{
static::addGlobalScope(
'current_user', function (Builder $builder) {
if (auth()->check()) {
$builder->where('created_by', auth()->id());
}
}
);
}
* Establishes the report template -> creator relationship.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
* Get the value of a checkbox field for the given field name.
*
* @param string $fieldName
* @param string $fallbackValue The value to return if the report template is not saved yet.
*/
public function checkmarkValue(string $fieldName, string $fallbackValue = '1'): string
{
if (is_null($this->id)) {
return $fallbackValue;
}
return $this->options[$fieldName] ?? '0';
}
* Get the value of a radio field for the given field name.
*
* @param string $fieldName
* @param string $value The value to check against.
* @param bool $isDefault Whether the radio input being checked is the default.
*/
public function radioValue(string $fieldName, string $value, bool $isDefault = false): bool
{
$fieldExists = array_has($this->options, $fieldName);
if (!$fieldExists && $isDefault) {
return true;
}
if ($fieldExists && $this->options[$fieldName] === $value) {
return true;
}
return false;
}
* Get the value of a select field for the given field name.
*
* @param string $fieldName
* @param string|null $model The Eloquent model to check against.
*
* @return mixed|null
*/
public function selectValue(string $fieldName, string $model = null)
{
if (!isset($this->options[$fieldName])) {
return null;
}
$value = $this->options[$fieldName];
if (is_array($value)) {
$value = $value[0];
}
if ($model) {
$foundModel = $model::find($value);
return $foundModel ? $foundModel->id : null;
}
return $value;
}
* Get the values of a multi-select field for the given field name.
*
* @param string $fieldName
* @param string|null $model The Eloquent model to check against.
*
* @return iterable
*/
public function selectValues(string $fieldName, string $model = null): iterable
{
if (!isset($this->options[$fieldName])) {
return [];
}
if ($model) {
return $model::findMany($this->options[$fieldName])->pluck('id');
}
if (!is_array($this->options[$fieldName])) {
return [$this->options[$fieldName]];
}
return $this->options[$fieldName];
}
* Get the value of a text field for the given field name.
*
* @param string $fieldName
* @param string|null $fallbackValue
*
* @return string
*/
public function textValue(string $fieldName, string|null $fallbackValue = ''): string
{
if (is_null($this->id)) {
return (string) $fallbackValue;
}
return $this->options[$fieldName] ?? '';
}
public function getDisplayNameAttribute()
{
return $this->name;
}
}