3
1

More than 3 years have passed since last update.

[Laravel]Non-static methodのエラーの対応方法

Posted at

エラー文

Non-static method App\Models\Person::getData() should not be called statically.

staticにしていないメソッドを呼び出せない。

対応方法

HelloController.php
use App\Models\Person;

class HelloController extends Controller
{
    public function index()
    {
        $data = Person::getData();
    }
}

⬇️

HelloController.php
use App\Models\Person;

class HelloController extends Controller
{
    public function index()
    {
        $person = new Person();
        $data = $person->getData();
    }
}

このようにモデルのインスタンスを作ってから呼び出すといけます。

モデル側を変更できるときは、functionにstaticをつけることでも対応できるみたいです。

Person.php
class Person extends Model
{
    public static function getData()
    {
       //

参考文献

Laravel:Non-static methodのエラーが発生した時の対応方法
PHP5.6で「Non-static method 【x】 should not be called statically in 【y】 on line 【z】」とかいうエラーが出た。

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