【问题标题】:Eloquent Carbon Error Trailling data when request existing data which is not inserted using eloquent请求未使用 eloquent 插入的现有数据时,Eloquent Carbon 错误跟踪数据
【发布时间】:2019-02-14 20:43:11
【问题描述】:

我正在尝试创建 REST Api 并遇到这个烦人的问题。我正在使用 odoo 数据库中的现有表,该表预先插入了几条记录。 而且我意识到每当我想从 odoo 检索现有行时,都会出现“Carbon Trailing Data”错误。但不是我通过 laravel eloquent 方法创建的记录。

我已经定义了日期变更器并将时间戳变更器转换为模型中表的正确字段。但它仍然没有让我到任何地方。除了更新/删除现有记录的日期/时间戳值之外,还有其他方法吗?

这是我的一些代码

型号

namespace App;

use App\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'hr_employee';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        // Private information
        'resource_id',
        'name',
        'user_id',
        'active',
        'address_home_id',
        'country_id',
        'gender',
        'marital',
        'spouse_complete_name',
        'spouse_birthday',
        'children',
        'place_of_birth',
        'country_of_birth',
        'birthday',
        'emergency_contact',
        'emergency_phone',
        'identification_id',
        'passport_id',
        'bank_account_id',
        'permit_no',
        'visa_no',
        'visa_expire',
        'additional_info',
        'km_home_work',
        'google_drive_link',
        'certificate',
        'study_field',
        'study_school',
        // Work information
        'company_id',
        'resource_calendar_id',
        'department_id',
        'job_id',
        'job_title',
        'work_phone',
        'mobile_phone',
        'work_email',
        'work_location',
        'notes',
        'parent_id',
        'coach_id',
        'barcode',
        'pin',
        'color',
        'last_attendance_id',
        'create_uid',
        'create_date',
        'write_uid',
        'write_date',
    ];

    const CREATED_AT = 'create_date';
    const UPDATED_AT = 'write_date';

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'birthday',
        'spouse_birthdate',
        'visa_expire',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'create_date' => 'timestamp',
        'write_date' => 'timestamp',
    ];

    public function Job() {
        return $this->belongsTo('App\Job', 'job_id');
    }

    public function getNameAcronymAttribute() {
        $words = explode(" ", $this->name);
        $acronym = "";

        foreach ($words as $w) {
            $acronym .= $w[0];
        }

        return $acronym;
    }
}

控制器显示动作

public function show($id)
{
    $httpCode = 200;
    $message = "Here is the employee data";
    $modelData = null;

    try {
        $modelData = Employee::find($id);

        if (!$modelData) {
            $httpCode = 404;
            $message = 'Employee data not found';
        }
    } catch (Exception $e) {
        $httpCode = $e->getCode();
        $message = $e->getMessage();
    }

    return response()->json([
        'message' => $message,
        'modelData' => $modelData
    ], $httpCode);
}

【问题讨论】:

    标签: laravel date timestamp laravel-5.7 php-carbon


    【解决方案1】:

    当您在 $dates 数组中指定一列时,laravel 会将这些列的值转换为 Carbon 实例。默认情况下,created_atupdated_at 列也会发生同样的情况。

    所以下面的所有列都必须是有效的时间戳列:

    protected $dates = [
         'birthday',
         'spouse_birthdate',
         'visa_expire',
     ];
    

    如果你的列不是时间戳,你可以使用casting

    protected $casts = [
        'birthday' => 'date:Y-m-d',
        'spouse_birthdate' => 'date:Y-m-d'
    ];
    

    【讨论】:

    • 这是最接近我的问题的答案,我只是意识到 postgresql 没有日期时间类型,它存储为时间戳,但实际上是日期时间。所以我只是试图将字段转换为日期时间,它的工作!感谢您的回答顺便说一句
    【解决方案2】:

    您没有显示任何代码。 但实际上,根据我过去的经验,昨天也有同样的问题,首先,使用碳解析创建日期的碳实例,以便在日期上连接任何其他碳辅助方法。 说你有一个像 = $date = 2019-02-14 的约会

    现在可以做类似的事情:

    $d = Carbon::parse($date);
    //hook any further carbon helper methods on the $d variable and it should work
    

    【讨论】:

    • 已编辑,包括模型和动作。我正在使用 eloquent 方法来检索数据,我应该定义访问器来修改日期字段值吗?
    • 也可以,只需将日期设置为Carbon实例
    猜你喜欢
    • 2015-09-19
    • 2019-06-24
    • 2021-12-02
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多