3

我以 UTC 格式保存所有日期时间数据(created_at、updated_at 和自定义日期时间列:约会_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 对 Note Model 进行了更正,并在下面添加了完整的模型(包括@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');
    }

}

有关错误的更多信息(伯尼更新后)

 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);
}   

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

4

1 回答 1

6

啊,现在我明白了!我意识到您正在使用Accessors 和 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);
}
于 2015-09-22T15:20:09.887 回答