使用 PHP 和 Laravel,为什么我不能将变量附加到数组中的值?
Using PHP and Laravel, why can i not append a variable to a value in an array?
我正在使用列表模型创建验证规则,我正在尝试将 date() 函数添加到数组的 key/value 对之一的值中:
class Listing extends Way\Database\Model {
public $today_date;
public function __construct()
{
$this->today_date = date("n/j/Y");
}
protected $guarded = ['id','created_at','updated_at'];
protected $table = 'listing';
protected static $rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after:'.$this->today_date,
];
}
当我提交表单时,我收到以下消息:
syntax error, unexpected '$this' (T_VARIABLE)
如何使用日期函数将其附加到数组中的值?
您不能在静态函数中使用 $this
(您的 $rules
是静态的)。
您可以只使用自定义验证
protected static $rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after_today
];
然后在您的应用中的某个地方
Validator::extend('after_today', function($attribute, $value, $parameters)
{
return ((strtotime($value)) > (strtotime('now')));
});
您不能使用 $this
来初始化 class 属性。
您可以在构造函数中执行此操作:
public function __construct()
{
$this->today_date = date("n/j/Y");
self::$rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after:' . $this->today_date,
];
}
我正在使用列表模型创建验证规则,我正在尝试将 date() 函数添加到数组的 key/value 对之一的值中:
class Listing extends Way\Database\Model {
public $today_date;
public function __construct()
{
$this->today_date = date("n/j/Y");
}
protected $guarded = ['id','created_at','updated_at'];
protected $table = 'listing';
protected static $rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after:'.$this->today_date,
];
}
当我提交表单时,我收到以下消息:
syntax error, unexpected '$this' (T_VARIABLE)
如何使用日期函数将其附加到数组中的值?
您不能在静态函数中使用 $this
(您的 $rules
是静态的)。
您可以只使用自定义验证
protected static $rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after_today
];
然后在您的应用中的某个地方
Validator::extend('after_today', function($attribute, $value, $parameters)
{
return ((strtotime($value)) > (strtotime('now')));
});
您不能使用 $this
来初始化 class 属性。
您可以在构造函数中执行此操作:
public function __construct()
{
$this->today_date = date("n/j/Y");
self::$rules = [
'deposit' => 'required|integer',
'date_available' => 'required|date|after:' . $this->today_date,
];
}