非静态方法不应静态调用,假设 $this 来自不兼容的上下文 -Laravel 5.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37416255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-static method should not be called statically, assuming $this from incompatible context -Laravel 5.2
提问by Sreejith Sasidharan
Hi I have the following models.
您好,我有以下型号。
Venues Model
场地模型
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');
}
}
Survey Model
调查模型
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');
}
}
When I am trying to get the surveys of a venue, I am getting following error.
当我尝试获取场地的调查时,出现以下错误。
$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
非静态方法 App\Venues::survey() 不应静态调用,假设 $this 来自不兼容的上下文
I have created Models and relations same like this in Laravel 5.1 without this issue. I want to know if I am missing something in Laravel 5.2.
我在 Laravel 5.1 中创建了相同的模型和关系,没有这个问题。我想知道我是否在 Laravel 5.2 中遗漏了什么。
采纳答案by Khalid Dabjan
survey()
is a relation method that is definitely not static, I don't really know what you are trying to accomplish but you have tow options, you either get the survey you want directly from the Survey
model like so:
survey()
是一种绝对不是静态的关系方法,我真的不知道您要完成什么,但是您有两个选择,您可以直接从Survey
模型中获得所需的调查,如下所示:
Survey::where('id','=', $id)->orderBy('surveys.id','DESC' )->first();
or you do that on an instance of the Venues
model like so:
或者你在Venues
模型的一个实例上这样做:
$venue->survey()->where('id','=', $id)->orderBy('surveys.id','DESC' )->first();
and in the second case of course it will be searching through the surveys that belong that specific venue.
当然,在第二种情况下,它将搜索属于该特定地点的调查。
回答by user1669496
If you are trying to get the surveys of the venue, then you should do something like this...
如果你想得到场地的调查,那么你应该做这样的事情......
$surveys = Venue::find($id)->venues()->orderBy('id', 'DESC')->first()