本文介绍了Laravel5-非静态方法不应该被静态调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道这是什么错误.请有人给我一些解释
I don't know what is this error. Please someone give me some explaination
在我的UserController.php
class UserController extends Controller {
public function viewCard($card_id) {
return Tag::test($card_id);
}
}
和我的模型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...
谢谢....
推荐答案
public function test()
不是静态方法.当您尝试使用Tag::test()
访问静态方法时,它将失败,因为..该方法不是静态的.
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.
您有两个选择:
1)将方法设置为静态
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)通过首先实例化您的类将其作为实例方法调用:
2) Invoke it as an instance method by first instantiating your class:
$tag = new Tag();
$tag->test($card_id);
这篇关于Laravel5-非静态方法不应该被静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!