Skip to content

Commit 5f7f63f

Browse files
committed
add append attributes docs
1 parent c0f76ca commit 5f7f63f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

6.x/crud-operation-fetch.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ public function fetchUser() {
8484
}
8585
```
8686

87+
**Adding attributes to fetched models without appends**
88+
89+
It's already possible to add attributes to the fetched models using the `appends` property in the model. However, this method has some drawbacks, like the fact that the appended attributes will be added to all instances of the model, whenever they are used, not only when they are fetched. If you want to add attributes to the fetched models without using the `appends` property in the model, you can use the `append_attributes` in the fetch configuration. For example:
90+
91+
```php
92+
public function fetchUser() {
93+
return $this->fetch([
94+
'model' => User::class,
95+
'append_attributes' => ['something'],
96+
]);
97+
}
98+
99+
// User.php model
100+
public function something(): Attribute
101+
{
102+
return Attribute::make(
103+
get: function (mixed $value, array $attributes) {
104+
return $attributes['something_else'];
105+
},
106+
);
107+
}
108+
109+
// and in your field definition
110+
CRUD::field('my_ajax_field')->attribute('something');
111+
```
112+
87113
<a name="fetch-ajax-filter"></a>
88114
## Using FetchOperation with `select2_ajax` filter
89115

7.x-dev/crud-operation-fetch.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ public function fetchUser() {
8484
}
8585
```
8686

87+
**Adding attributes to fetched models without appends**
88+
89+
It's already possible to add attributes to the fetched models using the `appends` property in the model. However, this method has some drawbacks, like the fact that the appended attributes will be added to all instances of the model, whenever they are used, not only when they are fetched. If you want to add attributes to the fetched models without using the `appends` property in the model, you can use the `append_attributes` in the fetch configuration. For example:
90+
91+
```php
92+
public function fetchUser() {
93+
return $this->fetch([
94+
'model' => User::class,
95+
'append_attributes' => ['something'],
96+
]);
97+
}
98+
99+
// User.php model
100+
public function something(): Attribute
101+
{
102+
return Attribute::make(
103+
get: function (mixed $value, array $attributes) {
104+
return $attributes['something_else'];
105+
},
106+
);
107+
}
108+
109+
// and in your field definition
110+
CRUD::field('my_ajax_field')->attribute('something');
111+
```
112+
87113
<a name="fetch-ajax-filter"></a>
88114
## Using FetchOperation with `select2_ajax` filter
89115

0 commit comments

Comments
 (0)