0

我正在尝试验证一些数据。我在 scotch.io 找到了这个教程。我在我的 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
    );

}
4

1 回答 1

0

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

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

Class CreateUser extends Migration或者Class UserModel extends Eloquent

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

User::$rules或者UserModel::$rules,只有当您更改了模型类名称时。

于 2014-12-31T07:10:53.247 回答