假设 $this 来自不兼容的上下文,不应静态调用非静态方法 -Laravel 5.2

Non-static method should not be called statically, assuming $this from incompatible context -Laravel 5.2

您好,我有以下型号。

场地模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Venues extends Model
{
    public $timestamps = false;
    /**
     * The database table used by the model.
     ** @var string
     */

    protected $table = 'locations';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'country','city','client_id'];


    public function survey(){
        return $this->belongsToMany('App\Survey', 'location_to_survey','location_id', 'survey_id')->withPivot('is_review');
    }

}

调查模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Survey extends Model
{
    //
    public function venues(){
        return $this->belongsToMany('App\Venues', 'location_to_survey','location_id', 'survey_id')->withPivot('is_review');
    }
}

当我尝试获取场地调查时,出现以下错误。

 $survey = Venues::survey()->where('id','=', $id)->orderBy('surveys.id','DESC' )->first();

Non-static method App\Venues::survey() should not be called statically, assuming $this from incompatible context

我在 Laravel 5.1 中创建了与此相同的模型和关系,但没有出现此问题。我想知道 Laravel 5.2 中是否遗漏了什么。

survey() 是一种绝对不是静态的关系方法,我真的不知道你想要完成什么,但你有两个选择,你可以直接从 Survey 像这样的模型:

Survey::where('id','=', $id)->orderBy('surveys.id','DESC' )->first();

或者你在 Venues 模型的一个实例上这样做:

$venue->survey()->where('id','=', $id)->orderBy('surveys.id','DESC' )->first();

当然,在第二种情况下,它将搜索属于该特定地点的调查。

如果你想得到场地的调查,那么你应该做这样的事情......

$surveys = Venue::find($id)->venues()->orderBy('id', 'DESC')->first()