gpt4 book ai didi

datetime - 使用 Laravel 5 中的日期修改器将检索到的日期时间数据转换为用户时区

转载 作者:行者123 更新时间:2023-12-04 01:59:34 26 4
gpt4 key购买 nike

我正在以 UTC 格式保存所有日期时间数据(created_at、updated_at 和自定义日期时间列:appointment_at)。

我想检索所有这些日期,并将它们转换为用户的时区以供显示。

我已将这些函数添加到我的模型中,认为这将检索用户时区中的日期时间:

public function getCreatedAtAttribute($value)
{
return $created_at->timezone(Auth::user()->timezone);
}

public function getUpdatedAtAttribute($value)
{
return $updated_at->timezone(Auth::user()->timezone);
}

public function getAppointmentAtAttribute($value)
{
return $appointment_at->timezone(Auth::user()->timezone);
}

但是我得到以下错误:

Call to a member function timezone() on a non-object

我猜我的语法有错误。我错过了什么吗?谢谢

更新 #1 更正了注释模型并在下方添加了完整模型(包括@bernie 的更正)

<?php namespace App;


use Illuminate\Database\Eloquent\Model;


class Note extends Model {


/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'notes';


/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['client_id', 'business_id', 'note'];


/**
* Note belonging to client.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function client()
{
return $this->belongsTo('App\Client');
}


public function getCreatedAtAttribute($value)
{
return $this->created_at->timezone(Auth::user()->timezone);
}

public function getUpdatedAtAttribute($value)
{
return $this->updated_at->timezone(Auth::user()->timezone);
}

public function getAppointmentAtAttribute($value)
{
return $this->appointment_at->timezone(Auth::user()->timezone);
}

数据库模型定义

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotesTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notes', function(Blueprint $table)
{
$table->increments('id');

$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');

$table->integer('business_id')->unsigned();
$table->foreign('business_id')->references('id')->on('businesses')->onDelete('cascade');

$table->text('note');
$table->dateTime('appointment_at');
$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('notes');
}

}

有关该错误的更多信息(由 bernie 更新后)

 ErrorException in Note.php line 40: Undefined property: App\Note::$created_at

in Note.php line 40
at HandleExceptions->handleError('8', 'Undefined property: App\Note::$created_at', '/var/www/html/laravel5/app/Note.php', '40', array()) in Note.php line 40
at Note->getCreatedAtAttribute('2015-09-22 12:50:20') in Model.php line 2669
at Model->mutateAttribute('created_at', '2015-09-22 12:50:20') in Model.php line 2592
at Model->getAttributeValue('created_at') in Model.php line 2557
at Model->getAttribute('created_at') in Model.php line 3263
at Model->__get('created_at') in NotesController.php line 54
at NotesController->show('1')

解决方案

public function getCreatedAtAttribute($value)
{
$date = $this->asDateTime($value);
return $date->timezone(\Auth::user()->timezone);
}

public function getCreatedAtAttribute($value)
{
$format = $this->getDateFormat();
return Carbon::createFromFormat($format, $value, 'UTC')->setTimezone( \Auth::user()->timezone);
}

这两种解决方案都对我有用。

最佳答案

啊,我明白了!我意识到你正在使用 Accessors and Mutators .您还覆盖了内置的 date mutators用于 created_at 和其他 dates 字段。

所以,试试这个:

public function getCreatedAtAttribute($value)
{
// asDateTime() is the built-in date mutator.
$date = $this->asDateTime($value);
return $date->timezone(Auth::user()->timezone);
}

public function getUpdatedAtAttribute($value)
{
$date = $this->asDateTime($value);
return $date->timezone(Auth::user()->timezone);
}

public function getAppointmentAtAttribute($value)
{
$date = $this->asDateTime($value);
return $date->timezone(Auth::user()->timezone);
}

关于datetime - 使用 Laravel 5 中的日期修改器将检索到的日期时间数据转换为用户时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32719004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com