-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingField.php
More file actions
69 lines (58 loc) · 1.85 KB
/
SettingField.php
File metadata and controls
69 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace App\Traits\Nova;
use App\Setting;
use Laravel\Nova\Http\Requests\NovaRequest;
trait SettingField
{
/**
* Resolve the field's value.
*
* @param mixed $resource
* @param string|null $attribute
* @return void
*/
public function resolve($resource, $attribute = null)
{
$this->resource = $resource;
$attribute = $attribute ?? $this->attribute;
if ($attribute === 'ComputedField') {
$this->value = call_user_func($this->computedCallback, $resource);
return;
}
if (! $this->resolveCallback) {
$setting = $this->component == 'boolean-field' ? (int) setting($attribute) : setting($attribute);
$this->value = $setting;
} elseif (is_callable($this->resolveCallback)) {
tap($this->resolveAttribute($resource, $attribute), function ($value) use ($resource, $attribute) {
$this->value = call_user_func($this->resolveCallback, $value, $resource, $attribute);
});
}
}
/**
* Hydrate the given attribute on the model based on the incoming request.
*
* @param NovaRequest $request
* @param string $requestAttribute
* @param object $model
* @param string $attribute
* @return void
*/
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
{
if ($request->exists($requestAttribute)) {
$value = $request[$requestAttribute];
$this->set($attribute, $this->isNullValue($value) ? null : $value);
}
}
/**
* @param string $key
* @param string|null $value
*/
protected function set(string $key, ?string $value)
{
Setting::updateOrCreate(
['key' => $key],
['value' => $value]
);
}
}