Php 在laravel 5的静态函数中调用非静态函数
我使用的是laravel 5。在一个模型中,我有一个静态函数,我在控制器中调用它。它工作得很好,但我希望在这个函数中对另一个非静态函数进行相同的更改,当我在静态函数中调用它时,它会产生错误Php 在laravel 5的静态函数中调用非静态函数,php,laravel-5,static-methods,static-functions,Php,Laravel 5,Static Methods,Static Functions,我使用的是laravel 5。在一个模型中,我有一个静态函数,我在控制器中调用它。它工作得很好,但我希望在这个函数中对另一个非静态函数进行相同的更改,当我在静态函数中调用它时,它会产生错误 Non-static method App\Models\Course::_check_existing_course() should not be called statically 这是我的模型 namespace App\Models; use Illuminate\Database\Eloquen
Non-static method App\Models\Course::_check_existing_course() should not be called statically
这是我的模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model {
public $course_list;
protected $primaryKey = "id";
public function questions(){
return $this->belongsToMany('App\Models\Question','course_questions')->where("status",1)->orderBy("id","DESC");
}
public static function courses_list(){
self::_check_existing_course();
}
private function _check_existing_course(){
if(empty($this->course_list)){
$this->course_list = self::where("status",1)->orderBy("course")->get();
}
return $this->course_list;
}
}
您将方法定义为非静态的,并且尝试将其作为静态调用
:
并将方法定义为静态->
公共静态函数课程\u列表(){
$courses=新课程();
$courses->_check_existing_courses();
}
通过读取代码,您试图做的是在对象上缓存查询结果 有两种方法可以解决这个问题:使用缓存外观() 或者,在这种特定情况下,如果您只想缓存该请求,则可以使用静态变量
class Course extends Model {
public static $course_list;
protected $primaryKey = "id";
public function questions(){
return $this->belongsToMany('App\Models\Question','course_questions')->where("status",1)->orderBy("id","DESC");
}
public static function courses_list(){
self::_check_existing_course();
}
private static function _check_existing_course(){
if(is_null(self::course_list) || empty(self::course_list)){
self::course_list = self::where("status",1)->orderBy("course")->get();
}
return self::course_list;
}
}
我试过了,但在静态函数$this->中不起作用。