> For the complete documentation index, see [llms.txt](https://crudstarter.gitbook.io/crudstarter-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crudstarter.gitbook.io/crudstarter-docs/generate-api-command.md).

# Generate API Command

## gen:api Command

{% hint style="info" %}
For laravel 11 use `php artisan install:api` command before this command.\
\
read more on <https://laravel.com/docs/11.x/routing#api-routes>
{% endhint %}

### Basic Usage

The CRUD Generator provides a single command, `gen:api`, that you can use to generate API resources for a given model. Here's the basic syntax:

```bash
php artisan gen:api {ModelName} --fields="fieldName:dataType fieldName:dataType"
```

Replace `{ModelName}` with the name of the model you want to generate CRUD resources for, and specify the fields for that model using the `--fields` option.

For example, to generate API resources for a `Post` model with fields `name` (string), `description` (text), and `count` (integer), you would run:

```bash
php artisan gen:api Post --fields="name:str description:text count:int"
```

### Interactive Questions

During the CRUD generation process, the command-line tool will ask you a few interactive questions to customize the generated code according to your preferences. These questions are:

The first question asks if you want to add the generate files in a specific folder:

```
Do you want to add controllers in a specific folder? (yes/no) [no]:
```

if you answer `no`, it will generate crud the default way

If you answer `yes`, it will prompt you to enter the folder name where you want to place the controllers:

```
Enter the Folder Name:
```

{% hint style="info" %}
Make sure Folder Name is in Capital Case ex:`Admin` not `admin`
{% endhint %}

<figure><img src="https://2199596568-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmEK8S3sawwZds4u2XuyZ%2Fuploads%2FUL9hU4dqI1FpBVYPTcKs%2FScreenshot%20from%202024-05-26%2020-44-22.png?alt=media&amp;token=074b5f34-dfc4-46f6-8399-9b3c827dcf4a" alt=""><figcaption><p>question example</p></figcaption></figure>

### API Options

The CRUD Generator package provides two options for generating APIs: with or without a model and migration.

1. **Generating API with Model and Migration**

This option is necessary when you don't have an existing CRUD or model for the resource you want to create an API for. It allows you to generate the model, migration, and API routes/controllers in a single command.

Use this option when:

* You're starting from scratch and need to create a new resource (model, migration, and API) from the ground up.
* You don't have an existing CRUD or model for the resource you need an API for.

<figure><img src="https://2199596568-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmEK8S3sawwZds4u2XuyZ%2Fuploads%2F1jzt837BdoQERRsiohuj%2Fapi_with_model_and_mig.png?alt=media&amp;token=803c9d0c-34ff-469f-b78e-e7c3a1c4b0e3" alt=""><figcaption></figcaption></figure>

2. **Generating API without Model and Migration**

This option is useful when you already have an existing CRUD and model for the resource, and you only need to generate the API routes and controllers.

Use this option when:

* You've already created a CRUD for the resource, and you want to add API functionality.
* The model and migration for the resource already exist.

By providing these two options, the CRUD Generator package offers flexibility in generating APIs based on your project's requirements. If you're starting fresh, you can use the first option to generate everything you need in a single command. If you're working with an existing CRUD, you can use the second option to add API functionality without duplicating the model and migration.

<figure><img src="https://2199596568-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmEK8S3sawwZds4u2XuyZ%2Fuploads%2F07hizXZFLvRetngBy6ER%2Fapi_without_model_and_mig.png?alt=media&amp;token=ac9f832d-0b53-4cc0-99ce-41edb92f7673" alt=""><figcaption></figcaption></figure>

### Adding Soft-Deletes in API

To add this functionality simply append `--softDelete` in gen command

```
php artisan gen:api Post --fields="name:str description:text" --softDelete
```

### Adding Relationships

To add this functionality simply add `--relations="your code here"` in gen command

> Example: To generate Profile API with relations (hasOne: Account, hasMany: Blogs and belongTo: User)

<pre><code><strong>php artisan gen:api Profile --fields="name:str user_id:fid" --relations="haso:account hasm:blogs belt:user"
</strong></code></pre>

| Relation Name | Shorthand For |
| ------------- | ------------- |
| haso          | hasOne        |
| hasm          | hasMany       |
| belt          | belongsTo     |
| belm          | belongsToMany |

{% hint style="info" %}
**Note**: you can use `hasMany`, `belongsTo` etc directly in --relations command if you feel comfortable and it currently only supports these 4 common relations type.
{% endhint %}

### Delete API Command

```
php artisan del:api ModelName
```

When generating an API using the CRUD Generator package, pay attention to the following:

1. **API Generation Folder**
   * If you want to generate the API inside a specific folder, you'll be prompted with a "yes/no" question.
   * If you answer "yes", you'll be asked to provide the folder name in the next command.
2. **Deleting the API**
   * Be cautious when deleting the generated API, as it will also delete the associated Model.
   * If the Model is being used by an existing CRUD, deleting the API will cause issues with the CRUD functionality.

<figure><img src="https://2199596568-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmEK8S3sawwZds4u2XuyZ%2Fuploads%2FZFKxP6BLzXpwXgVFthWo%2FScreenshot%20from%202024-06-02%2021-21-19.png?alt=media&amp;token=7a36e44e-7697-4177-ad60-2d7d51455b45" alt=""><figcaption><p>delete api command</p></figcaption></figure>

{% hint style="warning" %}
You need to delete migration and routes files manually !
{% endhint %}
