Php 用于获取/设置日期、小时、分钟的Laravel访问器变量
我的类中有一个名为start\u dt的datetime属性。保存时,需要将其保存为datetime,检索时,我需要日期、小时和分钟,以便我可以填充表单模型绑定刀片模板中的一些字段。相同的表单用于创建和更新 以我要求的格式检索此属性的正确方法是什么,如上文所述,即我需要仅使用start_dt属性的日期部分填充start_dt字段,但如果我们正在创建新用户,则它应默认为今天的日期。类似地,“开始时间”和“开始时间”下拉列表需要预先选择,如果是小时和分钟,则需要更新;如果我们正在创建新用户,则需要选择“00” 在数据库中,start_dt属性存储为日期时间值,例如2015-11-11 12:38:00。因此,当我们保存时,我需要更新从下拉列表中选择的小时和分钟。你怎么能做到这一点Php 用于获取/设置日期、小时、分钟的Laravel访问器变量,php,laravel-5.1,php-carbon,Php,Laravel 5.1,Php Carbon,我的类中有一个名为start\u dt的datetime属性。保存时,需要将其保存为datetime,检索时,我需要日期、小时和分钟,以便我可以填充表单模型绑定刀片模板中的一些字段。相同的表单用于创建和更新 以我要求的格式检索此属性的正确方法是什么,如上文所述,即我需要仅使用start_dt属性的日期部分填充start_dt字段,但如果我们正在创建新用户,则它应默认为今天的日期。类似地,“开始时间”和“开始时间”下拉列表需要预先选择,如果是小时和分钟,则需要更新;如果我们正在创建新用户,则需要选
class User extends Model {
protected $dates = ['start_dt'];
public function setStartDtAttribute($startDt, $startHr, $startMin)
{
$this->attributes['start_dt'] = ;
}
}
{!! Form::text('start_dt', isset($user->start_dt) ? $user->start_dt->format('d-m-Y') : Carbon::now()->format('d-m-Y'), ['class' => 'form-control']) !!}
{!! Form::select('start_hr', [
'00' => '00',
'01' => '01',
'02' => '02',
...
'23' => '23'], isset($user->start_dt) ? $user->start_dt->hour : '00', ['class' => 'form-control']) !!}
{!! Form::select('start_min', [
'00' => '00',
'01' => '01',
'02' => '02',
...
'59' => '59'], isset($user->start_dt) ? $user->start_dt->hour : '00', ['class' => 'form-control']) !!}
如果您在文档的下面查看,他们有以下示例:
return $user->deleted_at->getTimestamp();
因此,在mutator方法中,您可以:
public function getStartDtAttribute($value)
{
return Carbon::createFromTimestamp($value->getTimestamp());
}
现在,您有了一个碳对象,您可以通过使用$user->startDt->year
等工具访问年份、月份等
要为新的开始日期设置当前日期,可以创建新的日期时间:
...
$user = new User();
$user->startDt = Carbon::now();
...
或者,为了方便起见,您可以在迁移设置中创建一个默认值
Schema::table('users', function ($table) {
....
$table->dateTime('start_dt')->default(Carbon::now();
....
});
您可以选择在保存之前将最终字符串转换为DateTime对象。我希望这对你有帮助。只要阅读碳文档就行了,很容易。看起来您正在使事情变得过于复杂,不确定是什么变得过于复杂?如何将start_hr和start_min值传递给setStartDtAttribute方法。看起来它必须在控制器方法内部完成?很难理解您想要实现什么,但是所有这些数组分配看起来都是可以避免的(我可能完全错了)。是的,defo不要把逻辑放在模板中,你会遇到各种各样的麻烦,因为这可能是模型中最好的(或者控制器,但胖模型=瘦控制器更好)请参阅更新的帖子,了解我正在尝试做的解释。我想你不明白我正在尝试做什么。我不知道怎样才能说得更清楚。保存日期时,我需要从“开始”文本字段中获取日期部分,从“开始”下拉字段中获取小时,从“开始”下拉字段获取分钟。使用mutator保存时,如何附加所有字段以创建日期时间值?
public function setStartDtAttribute($startDt, $startHr, $startMin)
{
// i guess the $startDt is the column you want to update
$splitDateTime = explode(" ",$startDt);
$datePart = explode("-", $splitDateTime[0]);
$timePart = explode(":", $splitDateTime[1]);
// i dont understand the $startDT variable but the $startHr
// and $startMin i know its the hour and minutes part of the time
// Now that the string is splitted into array lets replace the
// parts that you want to replace
//index 0 of the $timePart is the hour and 1 is the minutes
$timePart[0] = $startHr;
$timePart[1] = $startMin;
// Now lets join the arrays(ie. $datePart and $timePart)
// to form the datetime
$datePart = implode("-",$datePart);
$timePart = implode(":",$timePart);
$dateTime = $datePart . " " . $timePart
$this->attributes['start_dt'] = $dateTime;
}