【问题标题】:Resolved ["Method ResponseFactory::toArray does not exist."]已解决 [“方法 ResponseFactory::toArray 不存在。”]
【发布时间】:2020-10-19 08:40:29
【问题描述】:

我正在使用 Laravel 7.6 和 Vue 2。

我正在尝试将 Laravel 集合转换为数组。 这是有效的原始代码。

  public function index()
{
    $books = Book::all()->toArray();
    return array_reverse($books);
}
  • 我需要把它转换成数组,因为它更容易用 vue 处理。

  • 首先我通过 profile_id 来查找它的所有教育

  • 然后我尝试将其转换为 vue 的数组。

    public function index($id)
    { 
    if ($education = Education::where('profile_id',  
    $id)->get()) {
    }
    return response()->toArray($education); ```
    
    

我收到错误消息:“方法 Illuminate\Routing\ResponseFactory::toArray 不存在。”

【问题讨论】:

    标签: arrays laravel vue.js


    【解决方案1】:

    那是因为您在响应上调用 toArray(),而您应该在 $education 集合上调用它。

    尝试: return response()->json($education->toArray());

    这将使用 $education 数组生成完整的 JSON 响应

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多