Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yanziyan's avatar

About Eloquent: API Resources

In the documentation it says

'When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users'

okey but ,

If we are not creating an api then I can't use it

for example

public function index(): Response
    {
        return Inertia::render('Admin/Roles/RoleIndex', [
            'roles' => RoleResource::collection(Role::all())
        ]);
    }

or How do I get the table columns I want without using Roleresource? what are my options

0 likes
4 replies
LaryAI's avatar
Level 58

If you are not building an API, you can still use Eloquent API Resources to transform your Eloquent models into arrays that can be used in your views. You can create a resource class for your model and define the fields that you want to include in the transformed array. Here's an example:

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

In your controller, you can use the resource class to transform your Eloquent models into arrays:

use App\Http\Resources\UserResource;
use App\Models\User;

public function index()
{
    $users = User::all();

    return view('users.index', [
        'users' => UserResource::collection($users),
    ]);
}

In this example, we're using the UserResource class to transform the User models into arrays that include the id, name, email, created_at, and updated_at fields. We're then passing the transformed arrays to the users.index view.

If you don't want to use a resource class, you can simply call the toArray method on your Eloquent models to get an array representation of the model:

$users = User::all()->map->toArray();

This will give you an array of arrays, where each inner array represents a user and includes all of the model's attributes. However, this approach doesn't give you as much control over the fields that are included in the array, so it's generally better to use a resource class.

MohamedTammam's avatar

You can still using it anyway. but there is not reason to use it if you're not send API-like response.

It's mainly created for API to manipulate or hide model columns for convenience and security working with APIs.

However, when building Inertia app, consider that you're working with APIs. You need to follow all roles about hiding sensitive data when sending it to the user.

yanziyan's avatar

You need to follow all roles about hiding sensitive data when sending it to the user.

I'm looking for the answer to this.

How can I do the same thing it does without using API Resource?

MohamedTammam's avatar

@yanziyan Using getters, only/map methods, etc. But there's no point to do it without resources. Resources are created for that, why not using them?

Please or to participate in this conversation.