Laravel Fractal Manager

I wrote a transformer class for outputting data in an API:

APPTRANSFORMER:

<?php
namespace App\Transformer;

use App\Classes\AED;
use League\Fractal\TransformerAbstract;

class AEDTransformer extends TransformerAbstract {
    public function transform(AED $aed) {
        return [
            'owner' => $aed->owner,
            'street' => $aed->street,
            'latitude' => $aed->latitude,
            'longitude' => $aed->longitude,
            'annotationType' => $aed->annotation_type
        ];
    }
}

And a controller method to get the data requested:

CONTROLLER:

// Show specific AED
public function show($id) {
    // Find AED by ID
    $aed = AED::find($id);
    $rawData = $this->respondWithItem($aed, new AEDTransformer);
    $meta = ['meta' => 'TestMeta'];
    $data = array_merge($rawData, $meta);

    if (!$aed) {
        return $this->respondNotFound("AED existiert nicht.");
    }

    return $data;
}

When I call the URL I get the error:

ErrorException in AEDTransformer.php line 16: Argument 1 passed to App\Transformer\AEDTransformer::transform() must be an instance of App\Classes\AED, null given, called in /home/vagrant/Projects/MFServer/vendor/league/fractal/src/Scope.php on line 307 and defined

AED CLASS:

<?php
namespace App\Classes;

use Illuminate\Database\Eloquent\Model;

class AED extends Model {

    protected $table = 'aeds';
    protected $fillable = ['owner', 'street', 'postal_code', 'locality', 'latitude', 'longitude', 'annotation_type'];
    public $timestamps = true;

    public $id;
    public $owner;
    public $object;
    public $street;
    public $postalCode;
    public $locality;
    public $latitude;
    public $longitude;
    public $annotation_type;
    public $distance;

    public function set($data) {
        foreach ($data as $key => $value) {
            $this->{$key} = $value;
        }
    }
}

I think it must be something with the "extends Model" in the AED Class but I do not see the reason why. It is just an extension. Or do I look at the wrong place and understand the message wrongly?

Answers


You are getting this error because $aed = AED::find($id); is returning null which means the record doesn't exists.

You could do this

public function show($id) {

   // Find AED by ID

    $aed = AED::find($id);

    if (!$aed) { //just to be sure
       return $this->respondNotFound("AED existiert nicht.");
    }


    $rawData = $this->respondWithItem($aed, new AEDTransformer);
    $meta = ['meta' => 'TestMeta'];
    $data = array_merge($rawData, $meta);

    return $data;
}

Need Your Help

Best practice for creating millions of small temporary objects

java garbage-collection

What are the "best practices" for creating (and releasing) millions of small objects?

How can I use css to make a row of boxes that do not wrap when browser shrinks?

html css row

I apologize if this is a debug my code question or already answered and I could not find it, I saw many similar things that I couldn't get to work.