为什么我会获得'访问未声明的静态属性? (Laravel)

时间:2014-10-23 10:53:18

标签: php laravel laravel-4 laravel-validation

我正在尝试验证一些数据。我在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
    );

}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我无法在网上找到解决方案,我用PHPStorm跟踪了问题,发现我的班级被定义为&#34; TWICE&#34;所以它正在阅读第一个而不是我想要的那个,这是第二个。

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

Class CreateUser extends MigrationClass UserModel extends Eloquent

然后根据您的更改使用模型的方法,因此您要么保留它

User::$rulesUserModel::$rules仅当您更改了模型类名称时才会这样做。