Laravel5 - Non-static method should not be called

2019-02-19 07:14发布

I don't know what is this error. Please someone give me some explaination

on my UserController.php

class UserController extends Controller {
    public function viewCard($card_id) {
        return Tag::test($card_id);
    }
}

and on my model Tag.php

class Tag extends Model {
    public function test($card_id){
        return DB::SELECT(DB::RAW("SELECT name FROM tagmap tm, tags t WHERE t.id = tm.tag_id AND tm.card_id = :card_id"), ['card_id'=>$card_id]);
    }
}

i don't know where it fails, where I do wrong...

thanks....

标签: php laravel-5
1条回答
Emotional °昔
2楼-- · 2019-02-19 07:30

public function test() is not a static method. When you try to access a static method with Tag::test() it will fail, because.. well the method isn't static.

You have two options:

1) Set your method to static

class Tag extends Model {
    public static function test($card_id){
        return DB::SELECT(DB::RAW("SELECT name FROM tagmap tm, tags t WHERE t.id = tm.tag_id AND tm.card_id = :card_id"), ['card_id'=>$card_id]);
    }
}

2) Invoke it as an instance method by first instantiating your class:

$tag = new Tag();
$tag->test($card_id);
查看更多
登录 后发表回答