php - Laravel Fractal Manager -
i wrote transformer class outputting data in 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 controller method data requested:
controller:
// show specific aed public function show($id) { // find aed 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 call url error:
errorexception in aedtransformer.php line 16: argument 1 passed app\transformer\aedtransformer::transform() must instance of app\classes\aed, null given, called in /home/vagrant/projects/mfserver/vendor/league/fractal/src/scope.php on line 307 , 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 $key => $value) { $this->{$key} = $value; } } } i think must "extends model" in aed class not see reason why. extension. or @ wrong place , understand message wrongly?
you getting error because $aed = aed::find($id); returning null means record doesn't exists.
you
public function show($id) { // find aed id $aed = aed::find($id); if (!$aed) { //just sure return $this->respondnotfound("aed existiert nicht."); } $rawdata = $this->respondwithitem($aed, new aedtransformer); $meta = ['meta' => 'testmeta']; $data = array_merge($rawdata, $meta); return $data; }
Comments
Post a Comment