Laravel 拉威尔4号:Can';我们不能将方法应用到模型中吗?
我总是犯错误Laravel 拉威尔4号:Can';我们不能将方法应用到模型中吗?,laravel,model,laravel-4,Laravel,Model,Laravel 4,我总是犯错误 Non-static method MyModel::getCollection() should not be called statically, assuming $this from incompatible context 即使只是空方法也会失败: <?php class MyModel extends Eloquent { public $table = 'my_table'; public $timestamps = false;
Non-static method MyModel::getCollection() should not be called statically, assuming $this from incompatible context
即使只是空方法也会失败:
<?php
class MyModel extends Eloquent {
public $table = 'my_table';
public $timestamps = false;
public function getCollection()
{
//return $this->...
}
}
您不能使用static(:
)语法调用非静态的方法,但在Laravel
中,您可以像这样声明作用域
方法:
public function scopeGetCollection($query)
{
// use $query
//return $query
}
Modelname::getCollection(); // Replace Modelname with real model name
然后您可以从控制器中调用它,如下所示:
public function scopeGetCollection($query)
{
// use $query
//return $query
}
Modelname::getCollection(); // Replace Modelname with real model name
这只是模型文件夹上的一个Laravel的东西还是到处都是?所以没有范围就没有乐趣了@用户3763555,这(scope)只是Laravel
雄辩模型中的功能,但这对PHP中的非Laravel编码也有效:“您不能使用静态(:)语法调用非静态方法”是的,但我说的是scope
方法,它只适用于Laravel
。