API资源-调用未定义的方法:: toArray()

时间:2019-03-05 00:42:21

标签: php laravel

我正在尝试更改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);

});

我的InboxType

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

错误

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

给出错误的行是

return parent::toArray($request);

1 个答案:

答案 0 :(得分:4)

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

INSERT INTO DRAW (idcliente, date draw, stage)
SELECT id client, now () AS date_sorteo, 'S1' AS stage FROM client ORDER BY rand () LIMIT 4