Skip to content

Commit 5ab568b

Browse files
committed
Merge branch 'master' into remove-overrides
2 parents 5c72673 + 09cf360 commit 5ab568b

File tree

142 files changed

+12006
-21101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+12006
-21101
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ labels: triage
2424

2525
??
2626

27+
### Is it a bug in the latest version of Backpack?
28+
29+
After I run ```composer update backpack/crud``` the bug... is it still there?
30+
2731
### Backpack, Laravel, PHP, DB version
2832

2933
When I run ```php artisan backpack:version``` the output is:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ src/public/packages/nestedSortable/package.json
2424
src/public/packages/source-sans-pro/package.json
2525
src/public/packages/tinymce/package.json
2626
src/public/packages/tinymce/composer.json
27+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ We've spend more than 10.000 hours creating, polishing and maintaining administr
135135
If you are looking for a developer/team to help you build an admin panel on Laravel, look no further. You'll have a difficult time finding someone with more experience & enthusiasm for admin panels. This is _what we do_. [Contact us](https://backpackforlaravel.com/need-freelancer-or-development-team).
136136

137137

138+
138139
[ico-version]: https://img.shields.io/packagist/v/dick/crud.svg?style=flat-square
139140
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
140141
[ico-downloads]: https://img.shields.io/packagist/dt/tabacitu/crud.svg?style=flat-square

package-lock.json

Lines changed: 11435 additions & 20624 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
"devDependencies": {
1313
"cross-env": "^7.0.3",
1414
"css-loader": "^5.2.6",
15-
"laravel-mix": "^6.0.19",
15+
"laravel-mix": "^6.0.27",
1616
"lodash": "^4.17.21",
1717
"pace": "0.0.4",
18-
"resolve-url-loader": "^2.3.2",
19-
"sass": "^1.32.12",
18+
"resolve-url-loader": "^4.0.0",
19+
"sass": "^1.37.0",
2020
"sass-loader": "^9.0.3",
21-
"vue-template-compiler": "^2.6.12"
21+
"vue-template-compiler": "^2.6.14"
2222
},
2323
"dependencies": {
2424
"@coreui/coreui": "^2.1.16",
2525
"@digitallyhappy/backstrap": "^0.3.4",
2626
"animate.css": "^3.7.2",
2727
"bootstrap": "^4.6.0",
28-
"bootstrap-colorpicker": "^3.3.0",
28+
"bootstrap-colorpicker": "^3.4.0",
2929
"bootstrap-datepicker": "^1.9.0",
3030
"bootstrap-daterangepicker": "^3.1.0",
3131
"bootstrap-iconpicker": "^1.8.2",
3232
"ckeditor": "^4.12.1",
33-
"cropperjs": "^1.5.11",
34-
"datatables.net": "^1.10.23",
33+
"cropperjs": "^1.5.12",
34+
"datatables.net": "^1.10.25",
3535
"datatables.net-bs4": "^1.10.23",
3636
"datatables.net-fixedheader": "^3.1.8",
37-
"datatables.net-fixedheader-bs4": "^3.1.8",
37+
"datatables.net-fixedheader-bs4": "^3.1.9",
3838
"datatables.net-responsive": "^2.2.7",
39-
"datatables.net-responsive-bs4": "^2.2.7",
39+
"datatables.net-responsive-bs4": "^2.2.9",
4040
"easymde": "^2.15.0",
4141
"jquery": "^3.6.0",
4242
"jquery-colorbox": "^1.6.4",
@@ -49,7 +49,7 @@
4949
"noty": "^3.2.0-beta",
5050
"pace-js": "^1.2.4",
5151
"pc-bootstrap4-datetimepicker": "^4.17.51",
52-
"perfect-scrollbar": "^1.5.0",
52+
"perfect-scrollbar": "^1.5.2",
5353
"places.js": "^1.19.0",
5454
"popper.js": "^1.16.1",
5555
"select2": "^4.0.13",
@@ -59,6 +59,6 @@
5959
"source-sans-pro": "^3.6",
6060
"summernote": "^0.8.18",
6161
"sweetalert": "^2.1.2",
62-
"tinymce": "^5.7.0"
62+
"tinymce": "^5.8.2"
6363
}
6464
}

src/app/Library/CrudPanel/Traits/Create.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public function create($data)
2525
{
2626
$data = $this->decodeJsonCastedAttributes($data);
2727
$data = $this->compactFakeFields($data);
28+
$data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data);
2829

2930
// omit the n-n relationships when updating the eloquent item
3031
$nn_relationships = Arr::pluck($this->getRelationFieldsWithPivot(), 'name');
32+
3133
$item = $this->model->create(Arr::except($data, $nn_relationships));
3234

3335
// if there are any relationships available, also sync those
@@ -219,7 +221,7 @@ private function getRelationDataFromFormData($data)
219221
foreach ($relation_fields as $relation_field) {
220222
$attributeKey = $this->parseRelationFieldNamesFromHtml([$relation_field])[0]['name'];
221223

222-
if (isset($relation_field['pivot']) && $relation_field['pivot'] !== true) {
224+
if (! is_null(Arr::get($data, $attributeKey)) && isset($relation_field['pivot']) && $relation_field['pivot'] !== true) {
223225
$key = implode('.relations.', explode('.', $this->getOnlyRelationEntity($relation_field)));
224226
$fieldData = Arr::get($relationData, 'relations.'.$key, []);
225227
if (! array_key_exists('model', $fieldData)) {
@@ -237,25 +239,4 @@ private function getRelationDataFromFormData($data)
237239

238240
return $relationData;
239241
}
240-
241-
public function getOnlyRelationEntity($relation_field)
242-
{
243-
$entity_array = explode('.', $relation_field['entity']);
244-
245-
$relation_model = $this->getRelationModel($relation_field['entity'], -1);
246-
247-
$related_method = Arr::last($entity_array);
248-
249-
if (! method_exists($relation_model, $related_method)) {
250-
if (count($entity_array) <= 1) {
251-
return $relation_field['entity'];
252-
} else {
253-
array_pop($entity_array);
254-
}
255-
256-
return implode('.', $entity_array);
257-
}
258-
259-
return $relation_field['entity'];
260-
}
261242
}

src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,6 @@ protected function makeSureFieldHasEntity($field)
151151
return $field;
152152
}
153153

154-
protected function makeSureFieldHasRelationshipData($field)
155-
{
156-
// only do this if "entity" is defined on the field
157-
if (! isset($field['entity'])) {
158-
return $field;
159-
}
160-
161-
$extraFieldAttributes = $this->inferFieldAttributesFromRelationship($field);
162-
163-
if (! empty($extraFieldAttributes)) {
164-
$field = array_merge($field, $extraFieldAttributes);
165-
} else {
166-
abort(500, 'Unable to process relationship data: '.$field['name']);
167-
}
168-
169-
return $field;
170-
}
171-
172154
protected function overwriteFieldNameFromEntity($field)
173155
{
174156
// if the entity doesn't have a dot, it means we don't need to overwrite the name

src/app/Library/CrudPanel/Traits/Relationships.php

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
67

78
trait Relationships
89
{
@@ -15,33 +16,19 @@ trait Relationships
1516
public function getRelationInstance($field)
1617
{
1718
$entity = $this->getOnlyRelationEntity($field);
18-
$entity_array = explode('.', $entity);
19-
$relation_model = $this->getRelationModel($entity);
20-
21-
$related_method = Arr::last($entity_array);
22-
if (count(explode('.', $entity)) == count(explode('.', $field['entity']))) {
23-
$relation_model = $this->getRelationModel($entity, -1);
24-
}
25-
$relation_model = new $relation_model();
26-
27-
//if counts are diferent means that last element of entity is the field in relation.
28-
if (count(explode('.', $entity)) != count(explode('.', $field['entity']))) {
29-
if (in_array($related_method, $relation_model->getFillable())) {
30-
if (count($entity_array) > 1) {
31-
$related_method = $entity_array[(count($entity_array) - 2)];
32-
$relation_model = $this->getRelationModel($entity, -2);
33-
} else {
34-
$relation_model = $this->model;
35-
}
36-
}
37-
}
38-
if (count($entity_array) == 1) {
39-
if (method_exists($this->model, $related_method)) {
40-
return $this->model->{$related_method}();
19+
$possible_method = Str::before($entity, '.');
20+
$model = $this->model;
21+
22+
if (method_exists($model, $possible_method)) {
23+
$parts = explode('.', $entity);
24+
// here we are going to iterate through all relation parts to check
25+
foreach ($parts as $i => $part) {
26+
$relation = $model->$part();
27+
$model = $relation->getRelated();
4128
}
42-
}
4329

44-
return $relation_model->{$related_method}();
30+
return $relation;
31+
}
4532
}
4633

4734
/**
@@ -70,6 +57,41 @@ public function inferRelationTypeFromRelationship($field)
7057
return Arr::last(explode('\\', get_class($relation)));
7158
}
7259

60+
public function getOnlyRelationEntity($relation_field)
61+
{
62+
$relation_model = $this->getRelationModel($relation_field['entity'], -1);
63+
$related_method = Str::afterLast($relation_field['entity'], '.');
64+
65+
if (! method_exists($relation_model, $related_method)) {
66+
return Str::beforeLast($relation_field['entity'], '.');
67+
}
68+
69+
return $relation_field['entity'];
70+
}
71+
72+
/**
73+
* Get the fields for relationships, according to the relation type. It looks only for direct
74+
* relations - it will NOT look through relationships of relationships.
75+
*
76+
* @param string|array $relation_types Eloquent relation class or array of Eloquent relation classes. Eg: BelongsTo
77+
*
78+
* @return array The fields with corresponding relation types.
79+
*/
80+
public function getFieldsWithRelationType($relation_types): array
81+
{
82+
$relation_types = (array) $relation_types;
83+
84+
return collect($this->fields())
85+
->where('model')
86+
->whereIn('relation_type', $relation_types)
87+
->filter(function ($item) {
88+
$related_model = get_class($this->model->{Str::before($item['entity'], '.')}()->getRelated());
89+
90+
return Str::contains($item['entity'], '.') && $item['model'] !== $related_model ? false : true;
91+
})
92+
->toArray();
93+
}
94+
7395
/**
7496
* Parse the field name back to the related entity after the form is submited.
7597
* Its called in getAllFieldNames().
@@ -96,6 +118,21 @@ public function parseRelationFieldNamesFromHtml($fields)
96118
return $fields;
97119
}
98120

121+
protected function changeBelongsToNamesFromRelationshipToForeignKey($data)
122+
{
123+
$belongs_to_fields = $this->getFieldsWithRelationType('BelongsTo');
124+
125+
foreach ($belongs_to_fields as $relation_field) {
126+
$relation = $this->getRelationInstance($relation_field);
127+
if (Arr::has($data, $relation->getRelationName())) {
128+
$data[$relation->getForeignKeyName()] = Arr::get($data, $relation->getRelationName());
129+
unset($data[$relation->getRelationName()]);
130+
}
131+
}
132+
133+
return $data;
134+
}
135+
99136
/**
100137
* Based on relation type returns the default field type.
101138
*

src/app/Library/CrudPanel/Traits/Update.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function update($id, $data)
2626
$data = $this->compactFakeFields($data);
2727
$item = $this->model->findOrFail($id);
2828

29+
$data = $this->changeBelongsToNamesFromRelationshipToForeignKey($data);
30+
2931
$this->createRelations($item, $data);
3032

3133
// omit the n-n relationships when updating the eloquent item

src/public/packages/backpack/base/css/bundle.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)