0

when i use api resource collection in postman with type GET it returns Property [name] does not exist on this collection instance i dont know why although i wrote everything correct please help

i make a collection folder and it returns info

note : when i return $instructors = User::where('type',3)->get(); it returns informations

here is my code

my route api.php

Route::resource('instructors',InstructorController::class);

my collection file

public function toArray($request)
{
    // return parent::toArray($request);

    return [
        'name' => $this->name,
        'email' => $this->email,
        'area_id' => $this->area_id,
        'whatsapp' => $this->whatsapp,
        'phone' => $this->phone,
        'description' => $this->description,
    ];
}

my controller

 public function index()
{

    $instructors = User::where('type',3)->get();
    $collection = new InstructorCollection($instructors);
    return response()->json(['data'=>$collection,'error']);
}

my table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 250);
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->bigInteger('area_id')->unsigned()->nullable();
        $table->foreign('area_id')->references('id')->on('areas')->onDelete('set null');
        $table->string('whatsapp')->nullable();
        $table->string('phone')->nullable();
        $table->string('facebook')->nullable();
        $table->tinyInteger('type');
        $table->text('description')->nullable();
        $table->integer('views')->default('0');
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });
}

2 Answers2

2

You're using a UserResourceCollection (that has access to a $this->collection method) and trying to access the entity properties, instead of using the normal UserResource::collection method, which maps and fills multiple UserResource for you, returning an array of resources.

Create a normal resource called UserResource and call UserResource::collection($instructors) from your controller method.

More information in: https://laravel.com/docs/9.x/eloquent-resources#resource-collections

pmcpimentel
  • 586
  • 3
  • 7
0

Extend Illuminate\Http\Resources\Json\JsonResource class, and your problem will be resolved. If you extend ResourceCollection class, code like the following.

public function toArray($request)
{
    return [
        'data' => $this->collection->map(function ($item) {
            return [
                'name' => $item->name,
                'email' => $item->email,
                'area_id' => $item->area_id,
                'whatsapp' => $item->whatsapp,
                'phone' => $item->phone,
                'description' => $item->description,
            ];
        }),
    ];
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Zain Amin
  • 15
  • 7