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?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire