在 resource 中使用 array_get () 调用 $this 的关联关系获取字段时返回为空 有什么好的办法么?

如下代码获取关联关系字段时 'brand1' 'brand2' 使用上边的可以获取到 使用下边的array_get 取不到值
???

   public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'brand1' =>  $this->series->brand->name ,
            'brand2' =>  array_get($this,'series.brand.name') ,
        ];
    }
jaak
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

今天也遇到这个 问题了 'brand1' => $this->series->brand->name , 无法获取 name 字段 ,但是可以dd 到内容, 不知道您最后如何解决的

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!