【问题标题】:Why do I get 'Access to undeclared static property? (Laravel)为什么我会得到“访问未声明的静态属性? (拉拉维尔)
【发布时间】:2014-12-31 15:10:53
【问题描述】:

我正在尝试验证一些数据。我在 scotch.io 找到了这个tutorial。我在我的 UsersController 中使用以下内容来尝试验证一些数据:

public function store(){

        $validator = Validator::make(Input::all(), User::$rules);

        return Redirect::action("UserController@index");

    }

但是,我不断收到错误消息“访问未声明的静态属性:User::$rules”。难道我做错了什么?我尝试使用'php artisan dump-autoload'

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    protected $fillable = array(
                          'username',
                          'forename',
                          'surname',
                          'email',
                          'telephone',
                          'password',
                          'admin',
                          'customer',
                          'verification_link'
                        ); 


    public static $rules = array(
        'name'             => 'required',                       // just a normal required validation
        'email'            => 'required|email|unique:ducks',    // required and must be unique in the ducks table
        'password'         => 'required',
        'password_confirm' => 'required|same:password'          // required and has to match the password field
    );

}

【问题讨论】:

  • 老实说,一切看起来都不错。再次尝试运行php artisan dump-autoload。也许您不小心在另一个文件夹中运行了它(之前发生在我身上..)。或者尝试使变量成为对象,而不是静态的。
  • 试过你的代码,没有出现错误。也许您可以将规则移到控制器中,因为它是负责验证的控制器,而不是模型

标签: php laravel laravel-4 laravel-validation


【解决方案1】:

我遇到了同样的问题,我在网上找不到适合我的解决方案,我用 PHPStorm 跟踪了这个问题,发现我的 Class 被定义为“TWICE”,所以它读取的是第一个而不是第一个我想要的是第二个。

您可能遇到的问题是您的迁移文件包含“用户”表的迁移文件,并将其类定义为Class User extends Migration {,并且您的模型中Class User 的定义将类似于Class User extends Eloquent因此解决方案是将其中一个更改为:

Class CreateUser extends MigrationClass UserModel extends Eloquent

然后根据你的变化使用Model的方法,所以你要么保留它

User::$rulesUserModel::$rules,前提是您更改了模型类名称。

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 2017-03-23
    • 1970-01-01
    • 2017-10-13
    • 2020-08-27
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    相关资源
    最近更新 更多