# Basics

> The primary feature of Laravel Rest Api is the ability to access and administrate your database records using Eloquent. Laravel Rest Api accomplishes this by allowing you to define a Rest "resource" that corresponds to each Eloquent model in your application.

## Defining Resources

By default, Rest resources are stored in the app/Rest/Resources directory of your application. You may generate a new resource using the rest:resource Artisan command:

```bash
php artisan rest:resource UserResource
```

You might now define the model property. This property tells Laravel Rest Api which Eloquent model the resource corresponds to:

```php
public static $model = \App\Models\User::class;
```

Freshly created Rest resources only contain an ID exposed field. Don't worry, we'll add more fields to our resource soon.

## Registering Resources

By default, resources are not automatically registered to let you take advantage of the logic.

First, you need to declare a Controller:

```bash
php artisan rest:controller UsersController
```

Then, specify the resource in your controller:

```php
class UsersController extends Controller
{
    /**
     * The resource the controller corresponds to.
     *
     * @var class-string<\Lomkit\Rest\Http\Resource>
     */
    public static $resource = \App\Rest\Resources\UserResource::class;
}
```

Since the basic usage of these will be on the api side, you can declare your controller in your `api.php` file:

```php [api.php]
use \Lomkit\Rest\Facades\Rest;

Rest::resource('users', \App\Rest\Controllers\UsersController::class)
```

Once your resources are registered, you can verify that by using `php artisan route:list`:

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      URI
    </th>
    
    <th>
      Name
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        api/users
      </code>
    </td>
    
    <td>
      <code>
        api.users.details
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        api/users/search
      </code>
    </td>
    
    <td>
      <code>
        api.users.search
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        api/users/actions/{action}
      </code>
    </td>
    
    <td>
      <code>
        api.users.operate
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        api/users/mutate
      </code>
    </td>
    
    <td>
      <code>
        api.users.mutate
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      DELETE
    </td>
    
    <td>
      <code>
        api/users
      </code>
    </td>
    
    <td>
      <code>
        api.users.destroy
      </code>
    </td>
  </tr>
</tbody>
</table>

## Soft Deletes

If you want to expose soft delete routes for one of your resources, you can achieve this by using the `withSoftDeletes' method while registering the resource:

```php [api.php]
use \Lomkit\Rest\Facades\Rest;

Rest::resource('users', \App\Rest\Controllers\UsersController::class)->withSoftDeletes()
```

You now have two more routes registered:

<table>
<thead>
  <tr>
    <th>
      Method
    </th>
    
    <th>
      URI
    </th>
    
    <th>
      Name
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        api/users/restore
      </code>
    </td>
    
    <td>
      <code>
        api.users.restore
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      DELETE
    </td>
    
    <td>
      <code>
        api/users/force
      </code>
    </td>
    
    <td>
      <code>
        api.users.force
      </code>
    </td>
  </tr>
</tbody>
</table>

If you don't want to expose all soft deletes routes you can specify so:

```php [api.php]
use \Lomkit\Rest\Facades\Rest;

Rest::resource('users', \App\Rest\Controllers\UsersController::class)->withSoftDeletes(['forceDelete', 'restore'])
```
