Sam*_*son 66 php laravel eloquent
我试图在我的控制器中加载我的模型并尝试这样做:
return Post::getAll();
Run Code Online (Sandbox Code Playgroud)
得到了错误 Non-static method Post::getAll() should not be called statically, assuming $this from incompatible context
模型中的函数如下所示:
public function getAll()
{
return $posts = $this->all()->take(2)->get();
}
Run Code Online (Sandbox Code Playgroud)
在控制器中加载模型然后返回其内容的正确方法是什么?
Rub*_*zzo 87
您将方法定义为非静态方法,并尝试将其作为静态方式调用.那说......
...如果要调用静态方法,则应使用::并将方法定义为static.
// Defining a static method in a Foo class.
public static function getAll() { /* code */ }
// Invoking that static method
Foo::getAll();
Run Code Online (Sandbox Code Playgroud)...否则,如果你想调用一个实例方法,你应该实例化你的类,使用->.
// Defining a non-static method in a Foo class.
public function getAll() { /* code */ }
// Invoking that non-static method.
$foo = new Foo();
$foo->getAll();
Run Code Online (Sandbox Code Playgroud)注意:在Laravel中,几乎所有Eloquent方法都返回模型的实例,允许您链接方法,如下所示:
$foos = Foo::all()->take(10)->get();
Run Code Online (Sandbox Code Playgroud)
在该代码中,我们通过Facade 静态调用该all方法.之后,所有其他方法都被称为实例方法.
kei*_*ics 29
为什么不尝试添加Scope?范围是Eloquent的一个非常好的特征.
class User extends Eloquent {
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
public function scopeWomen($query)
{
return $query->whereGender('W');
}
}
$users = User::popular()->women()->orderBy('created_at')->get();
Run Code Online (Sandbox Code Playgroud)
TL; DR。您可以通过将查询表示为MyModel::query()->find(10);来解决此问题MyModel::find(10);。
据我所知,从PhpStorm 2017.2代码检查失败的方法,如MyModel::where(),MyModel::find()等(检查此线程)。这可能会很烦人,当您尝试在提交代码之前尝试使用PhpStorm的Git集成时,PhpStorm不会停止抱怨这些静态方法调用警告。
解决这一问题的一种优雅方法(IMOO)是在有意义的地方显式调用::query()。这将使您受益于免费的自动完成和良好的查询格式。
$myModel = MyModel::find(10); // static call complaint
// another poorly formatted query with code inspection complaints
$myFilteredModels = MyModel::where('is_beautiful', true)
->where('is_not_smart', false)
->get();
Run Code Online (Sandbox Code Playgroud)
$myModel = MyModel::query()->find(10);
// a nicely formatted query with no complaints
$myFilteredModels = MyModel::query()
->where('is_beautiful', true)
->where('is_not_smart', false)
->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135712 次 |
| 最近记录: |