• <bdo id='0XOuY'></bdo><ul id='0XOuY'></ul>

    <small id='0XOuY'></small><noframes id='0XOuY'>

      <tfoot id='0XOuY'></tfoot>
      <i id='0XOuY'><tr id='0XOuY'><dt id='0XOuY'><q id='0XOuY'><span id='0XOuY'><b id='0XOuY'><form id='0XOuY'><ins id='0XOuY'></ins><ul id='0XOuY'></ul><sub id='0XOuY'></sub></form><legend id='0XOuY'></legend><bdo id='0XOuY'><pre id='0XOuY'><center id='0XOuY'></center></pre></bdo></b><th id='0XOuY'></th></span></q></dt></tr></i><div id='0XOuY'><tfoot id='0XOuY'></tfoot><dl id='0XOuY'><fieldset id='0XOuY'></fieldset></dl></div>

    1. <legend id='0XOuY'><style id='0XOuY'><dir id='0XOuY'><q id='0XOuY'></q></dir></style></legend>

        扩展/覆盖 Eloquent 创建方法 - 不能使静态方法非静

        时间:2023-09-23
      1. <i id='OpJh1'><tr id='OpJh1'><dt id='OpJh1'><q id='OpJh1'><span id='OpJh1'><b id='OpJh1'><form id='OpJh1'><ins id='OpJh1'></ins><ul id='OpJh1'></ul><sub id='OpJh1'></sub></form><legend id='OpJh1'></legend><bdo id='OpJh1'><pre id='OpJh1'><center id='OpJh1'></center></pre></bdo></b><th id='OpJh1'></th></span></q></dt></tr></i><div id='OpJh1'><tfoot id='OpJh1'></tfoot><dl id='OpJh1'><fieldset id='OpJh1'></fieldset></dl></div>

        <small id='OpJh1'></small><noframes id='OpJh1'>

            <tbody id='OpJh1'></tbody>
          <legend id='OpJh1'><style id='OpJh1'><dir id='OpJh1'><q id='OpJh1'></q></dir></style></legend>

          <tfoot id='OpJh1'></tfoot>

                • <bdo id='OpJh1'></bdo><ul id='OpJh1'></ul>

                • 本文介绍了扩展/覆盖 Eloquent 创建方法 - 不能使静态方法非静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我覆盖了 create() Eloquent 方法,但是当我尝试调用它时,我得到 Cannot make static method Illuminate\Database\Eloquent\Model::create() 类 MyModel 中的非静态.

                  I'm overriding the create() Eloquent method, but when I try to call it I get Cannot make static method Illuminate\Database\Eloquent\Model::create() non static in class MyModel.

                  我像这样调用 create() 方法:

                  I call the create() method like this:

                  $f = new MyModel();
                  $f->create([
                      'post_type_id' => 1,
                      'to_user_id' => Input::get('toUser'),
                      'from_user_id' => 10,
                      'message' => Input::get('message')
                  ]);
                  

                  MyModel 类中,我有这个:

                  And in the MyModel class I have this:

                  public function create($data) {
                      if (!NamespaceAuth::isAuthed())
                          throw new Exception("You can not create a post as a guest.");
                  
                      parent::create($data);
                  }
                  

                  为什么这不起作用?我应该改变什么才能让它工作?

                  Why doesn't this work? What should I change to make it work?

                  推荐答案

                  正如错误所说:IlluminateDatabaseEloquentModel::create() 方法是静态的,不能被重写为非静态.

                  As the error says: The method IlluminateDatabaseEloquentModel::create() is static and cannot be overridden as non-static.

                  所以实现它

                  class MyModel extends Model
                  {
                      public static function create($data)
                      {
                          // ....
                      }
                  }
                  

                  并通过 MyModel::create([...]);

                  您也可以重新考虑 auth-check-logic 是否真的是模型的一部分,或者更好地将其移至控制器或路由部分.

                  You may also rethink if the auth-check-logic is really part of the Model or better moving it to the Controller or Routing part.

                  更新

                  这种方法从 5.4.* 版本开始不起作用,而是按照 这个答案.

                  This approach does not work from version 5.4.* onwards, instead follow this answer.

                  public static function create(array $attributes = [])
                  {
                      $model = static::query()->create($attributes);
                  
                      // ...
                  
                      return $model;
                  }
                  

                  这篇关于扩展/覆盖 Eloquent 创建方法 - 不能使静态方法非静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将数据插入到 Laravel 中的数据透视表 下一篇:laravel 挂钩到 Eloquent 前和后保存每个模型

                  相关文章

                  最新文章

                  <legend id='s3yQl'><style id='s3yQl'><dir id='s3yQl'><q id='s3yQl'></q></dir></style></legend>

                    1. <small id='s3yQl'></small><noframes id='s3yQl'>

                    2. <i id='s3yQl'><tr id='s3yQl'><dt id='s3yQl'><q id='s3yQl'><span id='s3yQl'><b id='s3yQl'><form id='s3yQl'><ins id='s3yQl'></ins><ul id='s3yQl'></ul><sub id='s3yQl'></sub></form><legend id='s3yQl'></legend><bdo id='s3yQl'><pre id='s3yQl'><center id='s3yQl'></center></pre></bdo></b><th id='s3yQl'></th></span></q></dt></tr></i><div id='s3yQl'><tfoot id='s3yQl'></tfoot><dl id='s3yQl'><fieldset id='s3yQl'></fieldset></dl></div>

                      <tfoot id='s3yQl'></tfoot>
                      • <bdo id='s3yQl'></bdo><ul id='s3yQl'></ul>