Skip to content

Commit 65cf1b5

Browse files
committed
action canRunWhen
1 parent d423967 commit 65cf1b5

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Predefined Field Types](development/fields/predefined/)
1919
- [Resolution / Formatting](development/fields/resolution.md)
2020
- [Validation](development/fields/validation.md)
21+
- [Fill Underlying Attribute](development/fields/fill.md)
2122
* [**Authorization**](development/authorization/)
2223
- [Policies](development/authorization/policies.md)
2324
- [Hiding Resource](development/authorization/hiding-resource.md)

docs/development/fields/fill.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Fill Underlying Attribute
2+
3+
When you create or update a resource every attribute on the model will be filled with given attribute values sent in the request to update the underlying database table.
4+
5+
The `fillUsing` method allows you to customize how a field is filled before it is saved on the database. This method accepts a callback which receives the request, the eloquent model instance and the name of the given attribute:
6+
7+
```php
8+
use Carisma\Fields\Field;
9+
10+
Field::make('name')->fillUsing(function($request, $model, $attribute){
11+
$model->name = ucfirst($request->name);
12+
}),
13+
```
14+

src/Actions/Action.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ public function canRun(Closure $runCallback)
127127
return $this;
128128
}
129129

130+
/**
131+
* Authorize the action on the given model based on resource gate permission.
132+
*
133+
* @param string $ability
134+
* @return $this
135+
*/
136+
public function canRunWhen($ability)
137+
{
138+
$this->canRun(function ($request, $model) use ($ability) {
139+
return $request->user()->can($ability, $model);
140+
});
141+
142+
return $this;
143+
}
144+
130145
/**
131146
* Determine if the action is executable for the given request.
132147
*

0 commit comments

Comments
 (0)