0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Modelのメソッドをコントローラーで呼び出せなかった

Last updated at Posted at 2020-01-08

###状況
青本を読んでControllerを軽くしようとModel(Favorite)に関数(totalGood)を実装。
しかし、Controller側で呼び出すことができなかった。

間違っているコード

Controller
Favorite->totalGood($id)

正しいコード

Controller
Favorite::first()->totalGood($id)

###解決
間違っている方のコードでは、Modelがインスタンス化されていなかったのが問題点。
Modelの後に
::first()
::all()
::get()
を挿入してインスタンス化した後に、Model内のメソッドを呼び出すのが正解。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?