访问未声明的静态属性:Laravel 中的 User::$rules
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25887647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to undeclared static property: User::$rules in laravel
提问by user2983060
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
$user = User::find($id);
$user->update($input);
return Redirect::route('users.show', $id);
}
return Redirect::route('users.edit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
this is what the code i am having, when I attempt to update a record it shows the following error message. Access to undeclared static property: User::$rules
这就是我拥有的代码,当我尝试更新记录时,它显示以下错误消息。访问未声明的静态属性:User::$rules
the user model as follows
用户模型如下
<?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');
}
回答by Martin Bean
I imagine you have a property in your User model like this:
我想你的 User 模型中有一个这样的属性:
<?php
class User extends Eloquent {
public $rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
);
}
This is a class instance property, but you're trying to access it statically (the error messages tells you such).
这是一个类实例属性,但您正在尝试静态访问它(错误消息告诉您这样)。
Simply add the static
keyword to the property:
只需将static
关键字添加到属性中:
<?php
class User extends Eloquent {
public static $rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
);
}