【问题标题】:API Resources - Call to undefined method ::toArray()API 资源 - 调用未定义的方法 ::toArray()
【发布时间】:2019-07-26 09:17:15
【问题描述】:

我正在尝试更改 json 返回,因此我正在使用 API 资源 我的路线:

Route::get('inbox/all', function(){
     $user_id =  Auth::user()->id;
        $inboxtype =   Messages::where('receiver_id', $user_id)->with('sender')->with(['bookings' => function($query) {
                            $query->with('currency');
                        }])->with('item_address')->orderBy('id','desc');

      return new InboxType($inboxtype);

});

我的收件箱类型

 public function toArray($request)
   {
       return parent::toArray($request);
   }

错误

调用未定义的方法 Illuminate\Database\Eloquent\Builder::toArray()

给出错误的行是

return parent::toArray($request);

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    在您的 $inboxtype = .... 中,它缺少 ->get() 。所以应该是这样的:

    $inboxtype =   Messages::where('receiver_id', $user_id)
                   ->with('sender')
                   ->with(['bookings' => function($query) {
                                $query->with('currency');
                            }])
                   ->with('item_address')
                   ->orderBy('id','desc')
                   ->get();
    

    【讨论】:

    • 天哪,我太笨了,我也很累 9 小时工作加上 4 小时开发一个项目:S 谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 2019-03-06
    • 2023-03-16
    • 2018-05-22
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多