Skip to content

Commit 9f5d753

Browse files
authored
Merge pull request #2 from Applejack21/v1.0.2
more fixes and examples
2 parents 927e3d3 + 4d83330 commit 9f5d753

File tree

7 files changed

+140
-14
lines changed

7 files changed

+140
-14
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ As well as this, it'll generate validation rules for the model based on the tabl
1010
Requires [Laravel](https://laravel.com/ "Laravel").
1111
Use composer to install it as described below:
1212
```
13-
$ composer require applejack21/laravel-actions
14-
```
15-
16-
```json
17-
{
18-
"require": {
19-
"applejack21/laravel-actions": "*"
20-
}
21-
}
13+
composer require applejack21/laravel-actions
2214
```
2315

2416
<a name="usage"></a>
@@ -34,6 +26,10 @@ The command also has a few arguments you can pass to customise it:
3426

3527
The files are then put into the folder ``app\Actions\<model_name>``. If there are files already in this folder it'll prompt you to replace these with the ones generated instead.
3628

29+
<a name="examples"></a>
30+
## Examples
31+
See the ``examples`` folder for a list of action files that have been generated using this command based on the default User model from Laravel. I shall try my best to remember to update the examples alongside code changes.
32+
3733
<a name="suggestions"></a>
3834
## Suggestions
3935
You're free to fork this and modify the code as you wish to add in your own extra functionality. However, if you have any suggestions that should be added to this package, do create an issue with the suggestion or even a PR with your modified code!

src/Actions/MakeDeleteFile.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ public function execute(string $modelName, string $fileName, bool $permaDelete =
2828
2929
class Delete$modelName
3030
{
31-
public function execute($modelName $$lowerModel): $modelName
31+
public function execute($modelName $$lowerModel): $modelName|bool
3232
{
33-
$$lowerModel ->delete();
33+
\$deleted = $$lowerModel ->delete();
3434
35-
return tap($$lowerModel)->refresh();
35+
// return the model if is utilises soft deletes as the record still exists
36+
if(in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($$lowerModel)) && \$deleted === true) {
37+
return tap($$lowerModel)->refresh();
38+
}
39+
40+
return \$deleted;
3641
}
3742
}
3843
EOT;
@@ -46,11 +51,11 @@ public function execute($modelName $$lowerModel): $modelName
4651
4752
class Delete$modelName
4853
{
49-
public function execute($modelName $$lowerModel, bool $permaVariable = false): $modelName
54+
public function execute($modelName $$lowerModel, bool $permaVariable = false): $modelName|bool
5055
{
5156
$permaVariable ? $$lowerModel ->forceDelete() : $$lowerModel ->delete();
5257
53-
return tap($$lowerModel)->refresh();
58+
return $permaVariable ? true : tap($$lowerModel)->refresh();
5459
}
5560
}
5661
EOT;

src/Examples/CreateUser.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class CreateUser
9+
{
10+
public function execute(array $request): User
11+
{
12+
$this->validate($request);
13+
14+
$user = User::create([
15+
...$request,
16+
]);
17+
18+
return tap($user)->refresh();
19+
}
20+
21+
private function validate(array $request): array
22+
{
23+
return Validator::validate($request, [
24+
'name' => 'required|max:255',
25+
'email' => 'required|unique:users,email|max:255|email',
26+
'email_verified_at' => 'nullable',
27+
'password' => 'required|max:255|confirmed',
28+
'two_factor_secret' => 'nullable',
29+
'two_factor_recovery_codes' => 'nullable',
30+
'two_factor_confirmed_at' => 'nullable',
31+
'remember_token' => 'nullable|max:100',
32+
'current_team_id' => 'nullable',
33+
'profile_photo_path' => 'nullable|max:2048'
34+
]);
35+
}
36+
}

src/Examples/DeleteUser.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
7+
class DeleteUser
8+
{
9+
public function execute(User $user): User|bool
10+
{
11+
$deleted = $user->delete();
12+
13+
// return the model if is utilises soft deletes as the record still exists
14+
if(in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($user)) && $deleted === true) {
15+
return tap($user)->refresh();
16+
}
17+
18+
return $deleted;
19+
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
7+
class DeleteUser
8+
{
9+
/**
10+
* This option is recommended for models with soft deletes trait.
11+
*/
12+
public function execute(User $user, bool $perma = false): User|bool
13+
{
14+
$perma ? $user->forceDelete() : $user->delete();
15+
16+
return $perma ? true : tap($user)->refresh();
17+
}
18+
}

src/Examples/GetUsers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Collection;
7+
8+
class GetUsers
9+
{
10+
public function execute(): Collection
11+
{
12+
return User::all();
13+
}
14+
}

src/Examples/UpdateUser.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Actions\User;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class UpdateUser
9+
{
10+
public function execute(User $user, array $request): User
11+
{
12+
$this->validate($request, $user);
13+
14+
$user->update([
15+
...$request,
16+
]);
17+
18+
return tap($user)->refresh();
19+
}
20+
21+
private function validate(array $request, User $user): array
22+
{
23+
return Validator::validate($request, [
24+
'name' => 'required|max:255',
25+
'email' => 'required|unique:users,email|max:255|email',
26+
'email_verified_at' => 'nullable',
27+
'password' => 'required|max:255|confirmed',
28+
'two_factor_secret' => 'nullable',
29+
'two_factor_recovery_codes' => 'nullable',
30+
'two_factor_confirmed_at' => 'nullable',
31+
'remember_token' => 'nullable|max:100',
32+
'current_team_id' => 'nullable',
33+
'profile_photo_path' => 'nullable|max:2048'
34+
]);
35+
}
36+
}

0 commit comments

Comments
 (0)