Commands
Valravn arrives with a set of commands that makes generating files and
classes easier.
all commands accept two parameter. first one is namespace
and second one is
name
. in addition, sometimes for versioning we have a v
parameter as well.
namespace
parameter determine the group of the entity. for instance, we could
have a Blog
namespace and create entities like Post
or Comment
in it.
the v
parameter determines the version of entity. we can create a Blog
namespace with several entities in it but in different versions.
Controller
this command helps to generate controller classes.
1php artisan valravn:controller namespace name --v=1 --relations --actions --requests --resources
the generated classes structure should be like:
1app/
2└── Http/
3 ├── V1/
4 │ └── Namespace/
5 │ └── Name/
6 │ ├── NameController
7 │ ├── NameRelationsController
8 │ └── NameActionsController
9 ├── Resources/
10 │ └── V1/
11 │ └── Namespace/
12 │ └── Name/
13 │ ├── NameResource
14 │ └── NameCollection
15 └── Requests/
16 └── V1/
17 └── Namespace/
18 └── Name/
19 ├── NameStoreRequest
20 └── NameUpdateRequest
Controllers
Controllers command is a shorthand for valravn:controller
command that
generates controllers and other related classes at once.
1php artisan valravn:controllers namespace name --v=1 --requests --resources
Requests
to generate only request classes, use this command.
1php artisan valravn:requests namespace name --v=1
This command generates store
and update
requests by default. if you defined your entity with
a batch update route, you can create a batch update request using --batch-update
flag.
1php artisan valravn:requests namespace name --v=1 --batch-update
Relation
Using relations
command, you can generate your requests for handling relationships.
1php artisan valravn:relation namespace name related-namespace related-name --v=1
It's the basic form of relation
command.
--v
parameter is 1
and it's not necessary to pass each time.
After passing needed parameters, you should determine the relation type
between two entities. for instance, assume there is a BelongsToMany
relationship between posts
entity from blog
namespace and categories
entity from core
namespace.
1php artisan valravn:relation blog posts core categories --belongs-to-many
This will create a request file in app/Http/Requests/V1/Blog/Post/PostCategoriesRequest.php
path, and you can set up
your request then. if there are some pivot columns, you can define them in pivots
method.
1use App\Models\Blog\Post;
2use Hans\Valravn\Http\Requests\Contracts\Relations\BelongsToManyRequest;
3
4class PostCategoriesWithPivotRequest extends BelongsToManyRequest {
5
6 protected function model(): string {
7 return Post::class;
8 }
9
10 protected function pivots(): array {
11 return [
12 'order' => [ 'numeric', 'min:1', 'max:99' ],
13 'info' => [ 'string', 'max:128' ],
14 ];
15 }
16
17}
Available relation flags
are: --belongs-to-many
, --has-many
, --morphed-by-many
, --morph-to-many
, --morph-to
.
The usage of --morph-to
flag is a bit different. so let's go for having an example.
1php artisan valravn:relation core likes likable --morph-to
As we know, the morphTo
relationship is between one model and many other models, and there is not a target model to
refer to. so as third parameter, just pass the morphable relation name. then in the created request file, you can limit
the related entities.
There is a --with-pivot
flag that allows you to create a pivot migration file for your many-to-many relationship.
1php artisan valravn:relation blog posts core categories --belongs-to-many --with-pivot
Resources
This command generates only resource and resource collection classes.
1php artisan valravn:requests namespace name --v=1
Exceptions
The exceptions arrive in two different form. The first one is full form exception which enables you to keep all errors in one exception class. On the other hand, the compact form is way more simpler and just contain one error and is very useful when you doesn't have a bunch of errors.
1php artisan valravn:exception namespace name prefix
This command has namespace
and name
arguments as well but there is a third argument named prefix
. The prefix
will be used to prefix the error numbers and acts as namespace for error codes.
The generated exception class is available under determined folder:
1app/
2└── Exceptions/
3 └── Namespace/
4 └── Name/
5 └── NameException
for more information see this.
Migration
you can generate migration file using this command.
1php artisan valravn:migration namespace name
it will generate the migration file
in database/migrations/Namespace/date_create_samples_table.php
path.
Pivot
To create pivot migration file, just enter the base entity and the related one's names and namespaces.
1php artisan valravn:pivot namespace name related-namespace related-name
Model
it generates model and related classes for you.
1php artisan valravn:model namespace name --factory --seeder --migration
it will generate the migration file
in database/migrations/Namespace/date_create_samples_table.php
path.
Policy
Policy command generates a policy class.
1php artisan valravn:policy namespace name
the policy class will locate here
1app/
2└── Policies/
3 └── Namespace/
4 └── NamePolicy
Repository
This command generates repository and repository contract.
1php artisan valravn:repository namespace name
generated files should be there
1app/
2└── Repositories/
3 ├── Contracts/
4 │ └── Namespace/
5 │ └── INameRepository
6 └── Namespace/
7 └── NameRepository
Service
Service command generates your service classes such as relation and action services.
1php artisan valravn:service namespace name --relations --actions
generated files would be like:
1app/
2└── Services/
3 └── Namespace/
4 └── Name/
5 ├── NameCrudService
6 ├── NameRelationsService
7 └── NameActionsService
Entity
And in the end, we have this command to create all classes and files at once.
1php artisan valravn:entity namespace name prefix --v=1