3
2

More than 3 years have passed since last update.

Call to undefined method Illuminate\Database\Eloquent\Builder::toArray() の解決例

Last updated at Posted at 2020-06-03

エラー文の様子

スクリーンショット 2020-06-04 2.43.24.png

どんなエラー?

app/Http/Controllers/RecordTimingController
public function syousai_matome($id)
     {
       $third_operation_class_id = Operation::query()->where('record_timing_id',$id)->distinct()->select('third_operation_class');
       return $third_operation_class_id->toArray();
      }

上記のように、コントローラで構文的に問題なくデータベースから情報を取得しようとしたら出てきたものです。

原因

データベースの情報をクエリ(今回は抽出)した状態で止まっており、その抽出したものをコントローラに呼び出していなかったため起きました。

※クエリとは:データベース上で特定の条件に合致したデータを検索したり、置換や削除などを行ったりすること。

解決策

下記のように、クエリ(今回は抽出)した状態のものを->get()で取得すればエラーが解消されました。

app/Http/Controllers/RecordTimingController
public function syousai_matome($id)
     {
       $third_operation_class_id = Operation::query()->where('record_timing_id',$id)->distinct()->select('third_operation_class')->get();
       return $third_operation_class_id->toArray();
      }
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2