使用Laravel 5中的日期变更器,使用用户的时区转换日期时间数据

时间:2015-09-22 14:02:19

标签: datetime laravel-5 php-carbon

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

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

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

select distinct @name = product.name from ...
if (@@rowcount > 1) select 'Nothing'
else select @name 

但是我收到以下错误:

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

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

更新#1对笔记模型进行了更正并在下面添加了完整的模型(包括@bernie的更正)

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

数据库模型定义

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

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

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

这两种解决方案都适用于我。

1 个答案:

答案 0 :(得分:2)

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

所以,试试这个:

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