Skip to content

Commit 98ed04d

Browse files
committed
- Updated format method to include the model itself
1 parent 2db5893 commit 98ed04d

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

docs/basic-usage/model-export.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class UsersExport extends ModelExport
108108
return User::class;
109109
}
110110

111-
public function formatValue(string $attribute, mixed $value): mixed
111+
public function formatValue($model, string $attribute, mixed $value): mixed
112112
{
113113
if ($attribute == 'role' && $value instance of \App\Models\Role::class) {
114114
return $value->description;

src/ModelExport.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,39 @@ public function map($model): array
8282
$attributes = $model->only($this->allowedAttributes());
8383

8484
foreach ($attributes as $attribute => $value) {
85-
$attributes[$attribute] = $this->formatValue($attribute, $value);
85+
$attributes[$attribute] = $this->formatValue($model, $attribute, $value);
8686
}
8787

8888
return array_values($attributes);
8989
}
9090

91-
public function isAdminModel(string $attribute, mixed $value): bool
91+
public function isAdminModel($model, string $attribute, mixed $value): bool
9292
{
9393
return $value instanceof Model && method_exists($value, 'getAdminLinkNameAttribute');
9494
}
9595

96-
public function isAdminModelCollection(string $attribute, mixed $value): bool
96+
public function isAdminModelCollection($model, string $attribute, mixed $value): bool
9797
{
9898
if (! $value instanceof Collection) {
9999
return false;
100100
}
101101

102102
foreach ($value as $item) {
103-
if (! $this->isAdminModel($attribute, $item)) {
103+
if (! $this->isAdminModel($model, $attribute, $item)) {
104104
return false;
105105
}
106106
}
107107

108108
return true;
109109
}
110110

111-
public function formatValue(string $attribute, mixed $value): mixed
111+
public function formatValue($model, string $attribute, mixed $value): mixed
112112
{
113113
if ($value instanceof BackedEnum) {
114114
return $value->getEnumLabel();
115-
} elseif ($this->isAdminModelCollection($attribute, $value)) {
115+
} elseif ($this->isAdminModelCollection($model, $attribute, $value)) {
116116
return $value->implode('admin_link_name', ',');
117-
} elseif ($this->isAdminModel($attribute, $value)) {
117+
} elseif ($this->isAdminModel($model, $attribute, $value)) {
118118
return $value->admin_link_name;
119119
} elseif (is_bool($value)) {
120120
return $value ? 'True' : 'False';

0 commit comments

Comments
 (0)