milroyfraser/sarala

Collection of Models have empty properties

Closed this issue · 3 comments

Hi, really excited for the potential of the package!

I'm using the sarala-io/laravel-companion but with Laravel's API Resources for the API. My model is set up like so:

My model:

import Model from './BaseAdminModel';

export default class VendorRequest extends Model {
    resourceName () {
        return 'vendor-requests';
    }

    fields () {
        return [
            'vendor',
            'category',
            'status_code',
            'success',
            'response_attributes'
        ];
    }

    dates () {
        return {
            created_at: 'YYYY-MM-DD H:i:s',
            updated_at: 'YYYY-MM-DD H:i:s',
        };
    }
}

Usage in Vue Component:

//
getResources() {
    this.isLoading = true;
    const vendorRequest = new VendorRequest();

    vendorRequest.all().then((data) => {
        this.resources = data.data;
        console.log(data.data);
    }, () => {
        this.hasError = true;
        this.errorMessage = 'Could not retrieve resources';
    }).then(() => { this.isLoading = false; });
}
//

I can see the API response in the Network tab of Chrome:

{
  "data":[
    {
      "vendor":"odds-api",
      "category":"sports",
      "status_code":200,
      "success":true,
      "response_attributes":{
        "requests_remaining":"149",
        "requests_used":"1"
      },
      "created_at":"2018-03-25 06:06:44",
      "updated_at":"2018-03-25 06:06:46"
    },
    {
      "vendor":"odds-api",
      "category":"sports",
      "status_code":200,
      "success":true,
      "response_attributes":{
        "requests_remaining":"149",
        "requests_used":"1"
      },
      "created_at":"2018-03-25 06:53:35",
      "updated_at":"2018-03-25 06:53:37"
    }
  ]
}

And the console.log(data.data); within the Promise outputs correctly an array of two VendorRequest models, however the properties are not hydrated:

screen shot 2018-03-25 at 08 25 36

Any help would be much appreciated

This was my mistake, my API response (as you can see above) was not in the format required

I altered my API Resource to the following:

<?php

namespace App\Http\Resources;

use App\Models\VendorRequest;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin VendorRequest
 */
class VendorRequestResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'type' => 'vendor-requests',
            'attributes' => array_merge($this->only([
                'vendor',
                'category',
                'status_code',
                'success',
                'response_attributes'
            ]), [
                'created_at' => $this->created_at ? $this->created_at->toDateTimeString() : null,
                'updated_at' => $this->updated_at ? $this->updated_at->toDateTimeString() : null,
            ]),
        ];
    }
}

@mdcass if you are using laravel resources you will have to do this formatting. But if you are using sarala-io/laravel-companion you just have to implement a simple Transformer Class. sarala-io/laravel-companion will take care of formatting. And in your Controller

How to implement transformer

Thanks for the prompt response @milroyfraser

I have chosen to implement the logic within Laravel, which may itself be useful as a package to others - for others wanting to use Laravel's vanilla API Resources over Fractal's Transformers, here is the guide I followed.