Mar*_*lln 7 php laravel eloquent laravel-4
我正在压倒create()Eloquent方法,但是当我尝试调用它时,我得到了Cannot make static method Illuminate\\Database\\Eloquent\\Model::create() non static in class MyModel.
我这样称呼create()方法:
$f = new MyModel();
$f->create([
'post_type_id' => 1,
'to_user_id' => Input::get('toUser'),
'from_user_id' => 10,
'message' => Input::get('message')
]);
Run Code Online (Sandbox Code Playgroud)
在MyModel课堂上我有这个:
public function create($data) {
if (!Namespace\Auth::isAuthed())
throw new Exception("You can not create a post as a guest.");
parent::create($data);
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我应该改变什么来使它工作?
Hen*_*ash 33
正如错误所示:该方法Illuminate\Database\Eloquent\Model::create()是静态的,不能被重写为非静态方法.
所以实现它
class MyModel extends Model
{
public static function create($data)
{
// ....
}
}
Run Code Online (Sandbox Code Playgroud)
并称之为 MyModel::create([...]);
如果auth-check-logic实际上是模型的一部分或者更好地将其移动到Controller或Routing部分,您也可以重新考虑.
UPDATE
这种方法在版本5.4.*之后不起作用,而是遵循这个答案.
public static function create(array $attributes = [])
{
$model = static::query()->create($attributes);
// ...
return $model;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22970 次 |
| 最近记录: |