1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Related\Forms; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use SleepingOwl\Admin\Form\Related\Elements; |
7
|
|
|
|
8
|
|
|
class BelongsTo extends Elements |
9
|
|
|
{ |
10
|
|
|
protected $limit = 1; |
11
|
|
|
|
12
|
|
|
protected function proceedSave(\Illuminate\Http\Request $request) |
13
|
|
|
{ |
14
|
|
|
$relation = $this->getRelation(); |
15
|
|
|
|
16
|
|
|
// First we need to remove all entities |
17
|
|
|
if (! $this->toRemove->isEmpty()) { |
18
|
|
|
$class = get_class($relation->getRelated()); |
19
|
|
|
$class::destroy($this->toRemove->first()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if ($this->relatedValues->count() === 0) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** @var $relation \Illuminate\Database\Eloquent\Relations\BelongsTo */ |
27
|
|
|
$parent = $this->relatedValues->first(); |
28
|
|
|
$parent->save(); |
29
|
|
|
// Fresh is used to be sure, that we'll have all attributes loaded in model |
30
|
|
|
// (including autogenerated values by database engine) |
31
|
|
|
$child = $relation->associate($parent->fresh()); |
32
|
|
|
$child->save(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function prepareRelatedValues(array $data) |
36
|
|
|
{ |
37
|
|
|
$elements = $this->flatNamedElements($this->getNewElements()); |
38
|
|
|
foreach ($data as $relatedId => $attributes) { |
39
|
|
|
$related = $this->addOrGetRelated($relatedId); |
40
|
|
|
|
41
|
|
|
foreach ($elements as $element) { |
42
|
|
|
$attribute = $element->getModelAttributeKey(); |
43
|
|
|
$value = $element->prepareValue(array_get($attributes, $attribute)); |
|
|
|
|
44
|
|
|
$related->setAttribute($attribute, $value); |
|
|
|
|
45
|
|
|
$element->setModel($related); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function retrieveRelationValuesFromQuery($query): Collection |
51
|
|
|
{ |
52
|
|
|
$removeKeys = $this->toRemove->all(); |
53
|
|
|
$related = $this->getRelation()->getRelated(); |
54
|
|
|
|
55
|
|
|
return $query->get()->keyBy($related->getKeyName())->forget($removeKeys); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getModelForElements(): \Illuminate\Database\Eloquent\Model |
59
|
|
|
{ |
60
|
|
|
return $this->getEmptyRelation()->getRelated(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns fresh instance of model for each element in form. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
67
|
|
|
*/ |
68
|
|
|
protected function getFreshModelForElements(): \Illuminate\Database\Eloquent\Model |
69
|
|
|
{ |
70
|
|
|
$class = get_class($this->getEmptyRelation()->getRelated()); |
71
|
|
|
|
72
|
|
|
return new $class(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.