【发布时间】: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