ở đâu để làm cho các tập tin nếu tôi muốn sử dụng đặc điểm này trên mô hình của tôiLàm thế nào để thực hiện một Trait trong Laravel
Làm thế nào nên tập nhìn này như thế nào nếu tôi muốn có đặc điểm này bên trong:
trait FormatDates
{
protected $newDateFormat = 'd.m.Y H:i';
// save the date in UTC format in DB table
public function setCreatedAtAttribute($date){
$this->attributes['created_at'] = Carbon::parse($date);
}
// convert the UTC format to my format
public function getCreatedAtAttribute($date){
return Carbon::parse($date)->format($this->newDateFormat);
}
// save the date in UTC format in DB table
public function setUpdatedAtAttribute($date){
$this->attributes['updated_at'] = Carbon::parse($date);
}
// convert the UTC format to my format
public function getUpdatedAtAttribute($date){
return Carbon::parse($date)->format($this->newDateFormat);
}
// save the date in UTC format in DB table
public function setDeletedAtAttribute($date){
$this->attributes['deleted_at'] = Carbon::parse($date);
}
// convert the UTC format to my format
public function getDeletedAtAttribute($date){
return Carbon::parse($date)->format($this->newDateFormat);
}
}
Và làm thế nào để áp dụng nó cho ví dụ về một lớp người dùng mẫu ...
Tôi đã tạo ra các tập tin MyTraits.php và đó là trong thư mục App \ đặc điểm và trong Class tài khoản mẫu của tôi tôi có App \ Traits không gian tên; sử dụng App \ Traits \ FormatDates; sử dụng Notifiable, SoftDeletes, FormatDates; Và nó không hoạt động ... Trait không thể được tìm thấy ..... nếu bạn có thể giúp tôi xin vui lòng trên TeamViewer ??? – lewis4u
Đảm bảo bạn lưu tệp dưới dạng 'FormatDates.php' để khớp với tên lớp của bạn. –
Điều đó đã sửa nó .... bây giờ tôi gặp lỗi khác Lớp '\ App \ Người dùng' không tìm thấy .. ??? – lewis4u