Laravel nơi định dạng ngày

Ngày tháng có thể khó khăn, có rất nhiều định dạng để truyền hoặc chuyển đổi chúng thành. Trong tệp cấu hình


#[WithCast[DateTimeInterfaceCast::class, type: CarbonImmutable::class]]
public $date
9, có thể đặt định dạng ngày mặc định

    'date_format' => DATE_ATOM,

Bây giờ khi sử dụng

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
0 hoặc
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
0, định dạng được xác định sẽ được sử dụng

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date

Cũng có thể đặt định dạng theo cách thủ công;

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date

Khi truyền đối tượng dữ liệu sẽ sử dụng loại thuộc tính để truyền chuỗi ngày vào, vì vậy nếu bạn muốn sử dụng

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
1, điều đó hoàn toàn có thể

#[WithCast[DateTimeInterfaceCast::class]]
public Carbon $date

Bạn thậm chí có thể chỉ định thủ công loại chuỗi ngày sẽ được chuyển thành


#[WithCast[DateTimeInterfaceCast::class, type: CarbonImmutable::class]]
public $date

## Nhiều định dạng ngày

Đôi khi, ứng dụng của bạn có thể sử dụng các định dạng ngày khác nhau, ví dụ: bạn nhận được ngày từ ứng dụng iOS và React. Chúng sử dụng các định dạng ngày cơ bản khác nhau. Trong trường hợp này, bạn có thể thêm một mảng vào khóa

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
2 trong tệp cấu hình

#[WithCast[DateTimeInterfaceCast::class, type: CarbonImmutable::class]]
public $date
9

________số 8

Bây giờ khi truyền ngày, định dạng hợp lệ sẽ được tìm kiếm. Khi không tìm thấy gì, một ngoại lệ được đưa ra

Trong bài đăng này, chúng tôi sẽ xem xét mệnh đề where trong Laravel và cho bạn thấy một ví dụ sử dụng định dạng ngày []. Chúng ta sẽ xem qua mệnh đề where trong Laravel với một ví dụ về định dạng ngày []. Và làm thế nào để sử dụng nó, cũng như một cuộc biểu tình nếu cần thiết

Trong bài đăng này, tôi sẽ chỉ cho bạn cách sử dụng điều kiện where của trình tạo truy vấn Laravel với định dạng ngày []. Tôi chỉ cần tìm kiếm các bản ghi tháng và năm được chỉ định từ cột tại [dấu thời gian] được tạo khi tôi đang thực hiện dự án của mình. Tôi đã tự hỏi làm thế nào tôi có thể làm điều đó, mặc dù tôi đã quen thuộc với hàm date format[] của MySQL. Tuy nhiên, tôi không chắc cách sử dụng nó trong mệnh đề where của Laravel. Cuối cùng, tôi sử dụng DB của Laravel. raw[] và nó hoạt động

Trình truy cập và trình biến đổi cho phép bạn định dạng các giá trị thuộc tính Eloquent khi bạn truy xuất hoặc đặt chúng trên các phiên bản mô hình. Ví dụ: bạn có thể muốn sử dụng trình mã hóa Laravel để mã hóa một giá trị trong khi nó được lưu trữ trong cơ sở dữ liệu, sau đó tự động giải mã thuộc tính khi bạn truy cập nó trên mô hình Eloquent

Ngoài các bộ truy cập và trình biến đổi tùy chỉnh, Eloquent cũng có thể tự động truyền các trường ngày tháng sang các phiên bản Carbon hoặc thậm chí truyền các trường văn bản sang JSON

Bộ truy cập & bộ biến đổi

Định nghĩa một Accessor

Để xác định một trình truy cập, hãy tạo một phương thức

$firstName = $user->first_name;

0 trên mô hình của bạn trong đó

$firstName = $user->first_name;

1 là tên viết tắt "nghiên cứu" của cột bạn muốn truy cập. Trong ví dụ này, chúng ta sẽ định nghĩa một bộ truy cập cho thuộc tính

$firstName = $user->first_name;

2. Trình truy cập sẽ tự động được gọi bởi Eloquent khi cố truy xuất giá trị của thuộc tính

$firstName = $user->first_name;

2

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
3

Như bạn có thể thấy, giá trị ban đầu của cột được chuyển đến bộ truy cập, cho phép bạn thao tác và trả về giá trị. Để truy cập giá trị của bộ truy cập, bạn có thể chỉ cần truy cập thuộc tính

$firstName = $user->first_name;

2 trên một phiên bản mô hình

$firstName = $user->first_name;

Xác định một Mutator

Để xác định bộ biến đổi, hãy xác định phương thức

$firstName = $user->first_name;

5 trên mô hình của bạn trong đó

$firstName = $user->first_name;

1 là tên viết tắt "nghiên cứu" của cột bạn muốn truy cập. Vì vậy, một lần nữa, hãy xác định một trình biến đổi cho thuộc tính

$firstName = $user->first_name;

2. Bộ biến đổi này sẽ tự động được gọi khi chúng ta cố gắng đặt giá trị của thuộc tính

$firstName = $user->first_name;

2 trên mô hình

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
0

Trình biến đổi sẽ nhận giá trị đang được đặt trên thuộc tính, cho phép bạn thao tác giá trị và đặt giá trị được thao tác trên thuộc tính

$firstName = $user->first_name;

9 bên trong của mô hình Eloquent. Vì vậy, ví dụ: nếu chúng tôi cố gắng đặt thuộc tính

$firstName = $user->first_name;

2 thành
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
01

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
4

Trong ví dụ này, hàm

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
02 sẽ được gọi với giá trị
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
01. Trình biến đổi sau đó sẽ áp dụng hàm
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
04 cho tên và đặt giá trị kết quả của nó trong mảng

$firstName = $user->first_name;

9 bên trong

Người đột biến ngày

Theo mặc định, Eloquent sẽ chuyển đổi các cột

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
06 và
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
07 thành các phiên bản của Carbon, mở rộng lớp PHP
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
08 để cung cấp một loạt các phương thức hữu ích. Bạn có thể tùy chỉnh ngày nào được thay đổi tự động và thậm chí vô hiệu hóa hoàn toàn thay đổi này bằng cách ghi đè thuộc tính
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
09 của mô hình của bạn

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
3

Khi một cột được coi là ngày, bạn có thể đặt giá trị của nó thành dấu thời gian UNIX, chuỗi ngày [

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
40], chuỗi ngày-thời gian và tất nhiên là phiên bản
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
08 /
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
42 và giá trị của ngày sẽ tự động được lưu trữ chính xác trong cơ sở dữ liệu của bạn

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
7

Như đã lưu ý ở trên, khi truy xuất các thuộc tính được liệt kê trong thuộc tính

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
09 của bạn, chúng sẽ tự động được truyền sang phiên bản Carbon, cho phép bạn sử dụng bất kỳ phương thức nào của Carbon trên thuộc tính của mình

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
9

Định dạng ngày

Theo mặc định, dấu thời gian được định dạng là

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
44. Nếu bạn cần tùy chỉnh định dạng dấu thời gian, hãy đặt thuộc tính
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
45 trên mô hình của bạn. Thuộc tính này xác định cách các thuộc tính ngày được lưu trữ trong cơ sở dữ liệu, cũng như định dạng của chúng khi mô hình được tuần tự hóa thành một mảng hoặc JSON

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
2

Đúc thuộc tính

Thuộc tính

#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
46 trên mô hình của bạn cung cấp một phương pháp thuận tiện để chuyển đổi các thuộc tính thành các loại dữ liệu phổ biến. Thuộc tính
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
46 phải là một mảng trong đó khóa là tên của thuộc tính được truyền và giá trị là loại bạn muốn truyền cột tới. Các loại diễn viên được hỗ trợ là.
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
48,
#[WithCast[DateTimeInterfaceCast::class]]
#[WithTransformer[DateTimeInterfaceTransformer::class]]
public DateTime $date
49,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
30,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
31,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
32,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
33,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
34,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
35,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
36,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
37,
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
38, và
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
39

Ví dụ: hãy truyền thuộc tính

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
70, được lưu trữ trong cơ sở dữ liệu của chúng tôi dưới dạng số nguyên [
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
71 hoặc
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
72] thành giá trị boolean


#[WithCast[DateTimeInterfaceCast::class, type: CarbonImmutable::class]]
public $date
0

Giờ đây, thuộc tính

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
70 sẽ luôn được chuyển thành kiểu boolean khi bạn truy cập nó, ngay cả khi giá trị cơ bản được lưu trữ trong cơ sở dữ liệu dưới dạng số nguyên

Truyền mảng & JSON

Kiểu truyền

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
35 đặc biệt hữu ích khi làm việc với các cột được lưu trữ dưới dạng JSON tuần tự hóa. Ví dụ: nếu cơ sở dữ liệu của bạn có loại trường
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
75 hoặc
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
76 chứa JSON được tuần tự hóa, thì việc thêm biến
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
35 vào thuộc tính đó sẽ tự động giải tuần tự hóa thuộc tính thành một mảng PHP khi bạn truy cập nó trên mô hình Eloquent của mình


#[WithCast[DateTimeInterfaceCast::class, type: CarbonImmutable::class]]
public $date
0

Khi cast được xác định, bạn có thể truy cập thuộc tính

#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
78 và nó sẽ tự động được giải tuần tự hóa từ JSON thành một mảng PHP. Khi bạn đặt giá trị của thuộc tính
#[WithCast[DateTimeInterfaceCast::class, format: DATE_ATOM]]
#[WithTransformer[DateTimeInterfaceTransformer::class, format: DATE_ATOM]]
public DateTime $date
78, mảng đã cho sẽ tự động được tuần tự hóa lại thành JSON để lưu trữ

Làm cách nào để sử dụng định dạng ngày trong Laravel?

Tôi sẽ chỉ cho bạn mã điều khiển mà không cần đặt. .
Laravel 9 Thay đổi định dạng ngày với Model. sử dụng Ứng dụng\Mô hình\Người dùng;.
Laravel 9 Thay đổi định dạng ngày Y-m-d H. i. s sang d-m-Y. .
Laravel 9 Thay đổi định dạng ngày Y-m-d thành m/d/Y. .
Laravel 9 Thay đổi định dạng ngày m/d/Y thành Y-m-d. .
Laravel 9 Thay đổi định dạng ngày Y-m-d thành d/m/Y

Làm cách nào để kiểm tra định dạng ngày trong Laravel?

Làm cách nào để xác thực định dạng ngày tháng trong Laravel? . Trình xác thực này hỗ trợ tất cả các định dạng được hỗ trợ bởi phương thức createFromFormat[] lớp DateTime của PHP. using the date_format validator. This validator supports all formats supported by PHP's DateTime class createFromFormat[] method.

Làm cách nào để thay đổi định dạng ngày trong truy vấn Laravel?

Bạn luôn có thể sử dụng Định dạng -> của carbon['m/d/Y']; để thay đổi định dạng. Hoặc bạn chỉ có thể sử dụng selectRaw để tạo các truy vấn của mình. Lưu câu trả lời này.

Làm cách nào để thay đổi định dạng ngày trong Laravel 8?

Thay đổi ví dụ về định dạng ngày của Laravel . Tiếp theo, chúng tôi đã tạo một hàm newDateFormat[] trong phương thức này, bạn sẽ phải viết logic để thay đổi định dạng ngày bằng Carbon.

Chủ Đề