Eloquent 请求未使用 eloquent 插入的现有数据时的碳错误尾随数据

Eloquent Carbon Error Trailling data when request existing data which is not inserted using eloquent

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

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

这是我的一些代码

型号

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

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

现在可以做类似的事情:

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

当您在 $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'
];