dtx63505 2015-09-30 15:30
浏览 60

Laravel分形经理

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?

  • 写回答

1条回答 默认 最新

  • douke3007 2015-11-27 13:23
    关注

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的