Skip to content

Response Resources

Laraventus provides a powerful and type-safe way to structure your API responses using Resources. A resource defines exactly what data should be returned to the frontend, ensuring type clarity and automatic $type injection for AventusJs.

To create a simple response resource, define a class that extends AventusResource. All public properties of the class will automatically be included in the JSON response, along with the $type field.

<?php
namespace App\Http\Controllers\HelloWorld;
use Aventus\Laraventus\Resources\AventusResource;
/**
* @extends AventusResource
*/
class Response extends AventusResource
{
public function __construct(
public string $msg
) {}
}

Output

{
"result": {
"$type": "App.Http.Controllers.HelloWorld.Response",
"msg": "Hello World"
},
"errors": [],
"$type": "Aventus.Laraventus.Helpers.LaravelResult"
}

In many cases, you’ll want your resource to represent a Laravel Eloquent model. For this, Laraventus provides AventusModelResource, which lets you easily bind model data to your resource properties.

<?php
namespace App\Http\Resources;
use App\Models\User;
use Aventus\Laraventus\Resources\AventusModelResource;
/**
* @extends AventusModelResource<User>
*/
class UserResource extends AventusModelResource {
public string $name;
public string $email;
protected function bind($item): void {
$this->name = $item->name;
$this->email = $item->email;
}
}

Here:

  • The bind() method is automatically called with the model instance.
  • You can map only the fields you want to expose, keeping sensitive data private.

Output

{
"result": {
"$type": "App.Http.Resources.UserResource",
"name": "John Doe",
"email": "john@example.com"
},
"errors": [],
"$type": "Aventus.Laraventus.Helpers.LaravelResult"
}

If your resource properties match your model’s attributes, Laraventus can automatically bind them for you. To do this, extend AventusAutoBindResource.

<?php
namespace App\Http\Resources;
use App\Models\User;
use Aventus\Laraventus\Resources\AventusAutoBindResource;
/**
* @extends AventusAutoBindResource<User>
*/
class UserResource2 extends AventusAutoBindResource {
// These will be automatically bound
public string $name;
public string $email;
// Custom properties can still be added
public string $token;
protected function bind($item): void {
$this->token = md5($item->name . ' ' . $item->email);
}
}

In this example:

  • name and email are automatically filled from the User model.
  • token is a computed field set manually in the bind() method.

Output

{
"result": {
"$type": "App.Http.Resources.UserResource2",
"name": "John Doe",
"email": "john@example.com",
"token": "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
},
"errors": [],
"$type": "Aventus.Laraventus.Helpers.LaravelResult"
}

Both AventusModelResource and AventusAutoBindResource can be used to transform collections of models. You can use the static collection() method to convert an array or Eloquent collection into a list of typed resources.

$users = User::all();
return UserResource::collection($users);

Each item in the resulting array will be wrapped with the proper $type field, ensuring that AventusJs can recreate the correct object types on the frontend.

Output

{
"result": [
{
"$type": "App.Http.Resources.UserResource",
"name": "Alice",
"email": "alice@example.com"
},
{
"$type": "App.Http.Resources.UserResource",
"name": "Bob",
"email": "bob@example.com"
}
],
"errors": [],
"$type": "Aventus.Laraventus.Helpers.LaravelResult"
}

This makes it effortless to return entire model lists while keeping responses fully typed and consistent.