Laravel Tinker là gì

Trong bài viết này, tôi xin giới thiệu với tất cả các bạn đang đọc một tính năng ít được biết đến của Laravel, tính năng này giúp bạn nhanh chóng đọc được dữ liệu ngay bên trong dự án của mình. That is Tinker of Laravel, to use Tinker run command

  php artisan tinker

Tinker của Laravel artisan là một vòng lặp lặp lại [read-eval-print]

Ví dụ, tôi có một vài câu lệnh xử lý Cơ sở dữ liệu như sau

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;

Muốn xem kết quả của những câu hỏi trên, nhanh nhất chúng ta cũng cần tới vài thao tác xử lý. Tạo Controller, route, Model,. v. v. Tuy nhiên, với php artisan tinker, chúng ta có thể xem được kết quả đầu ra vô cùng nhanh chóng. Tinker cho phép chúng ta tương tác trực tiếp với ứng dụng của mình thông qua các lệnh như stop[] hay dd[] thậm chí chí là cả print_r[]

Hướng dẫn sử dụng

  • Cài đặt dự án Laravel. To process for the before the Tinker or make a project of your own
  composer create-project laravel/laravel TinkerTest --prefer-dist
  • Thiết lập cơ sở dữ liệu. Di chuyển đang chạy

Sau khi tạo thành công một dự án mới, bạn cần tạo cơ sở dữ liệu mới và cài đặt di chuyển. Trong bài viết này, tôi sẽ sử dụng các lần di chuyển mặc định của Laravel. Hãy đảm bảo rằng bạn đã cài đặt trong tệp. env to could run been migrations. Laravel đã tạo sẵn cho chúng ta migrations of 2 table are users and password_resets. Tiến hành chạy migrations, from root run command

php artisan migrate
  • Tạo cơ sở dữ liệu của chúng tôi

Laravel cung cấp cho chúng ta chức năng model factory cái mà chúng ta sử dụng để tiến hành seed database. Bây giờ, hãy bắt đầu với Tinker

From root directory of project, run command

php artisan tinker

Lệnh này sẽ mở một relp, với relp vừa được hiển thị, chúng ta có thể chạy model factory và tiến hành cơ sở dữ liệu hạt giống tại đó bằng lệnh

factory[App\User::class, 10]->create[];

Sau khi chạy xong câu lệnh trên trong relp, để chắc chắn rằng câu lệnh đã được thực thi, hãy truy cập cơ sở dữ liệu và kiểm tra lại. Nếu người dùng mới được sinh ra tức thì câu lệnh đã chạy đúng. Một bộ sưu tập của 10 người dùng sẽ hiển thị bên trên màn hình nếu bạn nhập lệnh

App\User::all[];

Nếu muốn đếm tổng số lượng bản ghi có trong bản người dùng, hãy chạy lệnh

App\User::count[];

Sau khi chạy hai lệnh trên, relp sẽ hiển thị như sau

  • Tạo người dùng mới

Với relp, chúng ta cũng có thể tạo mới được một bản ghi trong bảng người dùng, lưu ý rằng khi nhập mã trong relp cũng tương tự như bạn đang nhập mã trong dự án của mình vậy. To create new user done as after

________số 8

Vui lòng thử nhập $user trong relp, thông tin user vừa được khai báo sẽ hiển thị trên màn hình

  • Xóa người dùng

To delete a user in the user table, thao tác;

$user = App\User::find[1];
$user->delete[];
  • Đọc tài liệu chức năng/lớp

Với Tinker, bạn có thể xem tài liệu của một lớp hoặc một chức năng. Tuy nhiên, nó phụ thuộc vào hàm hay là hàm của bạn có sử dụng DocBlocks hay không?

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
0

Ví dụ, tôi muốn xem tài liệu của hàm dd[]

Nếu muốn chi tiết hơn, hãy xem mã nguồn của một hàm hoặc một lớp

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
1

Tôi muốn xem hàm dd[] viết cái gì ở trong

Trên đây chỉ là một vài chức năng của Tinker khi làm việc với cơ sở dữ liệu, ngoài ra còn nhiều tính năng vô cùng hữu ích khác nữa. Hãy cùng tìm hiểu và chia sẻ nhé. ^^

Khả năng tương tác dễ dàng với ứng dụng của bạn để thử nghiệm hoặc gỡ lỗi các sự cố là một điều cực kỳ quan trọng. Để giải quyết vấn đề này, Laravel cung cấp một lệnh

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
20 ngay lập tức. Lệnh
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
20 sử dụng thư viện PsySH để thiết lập môi trường REPL [read-eval print loop] trong ngữ cảnh ứng dụng Laravel của bạn [chương này có thể đề cập đến Tinker, Tinker REPL hoặc môi trường Tinker; tất cả đều đề cập đến cùng một thứ]. Tinker REPL cho phép bạn tương tác với ứng dụng của mình theo cách không khác với cách tương tác với mã phía máy khách bằng cách sử dụng các sản phẩm như Công cụ dành cho nhà phát triển của Chrome

Để bắt đầu sử dụng Tinker, chỉ cần đưa ra lệnh

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
20 Artisan trong thư mục ứng dụng gốc

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
5

Nếu phiên Tinker bắt đầu thành công, bảng điều khiển sẽ hiển thị đầu ra tương tự như sau

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
2

#Thực thi Tuyên bố mã tùy ý

Tinker REPL cho phép bạn nhập mã PHP và đánh giá mã đó ngay lập tức, tương tự như cách Công cụ dành cho nhà phát triển của Chrome cho phép các nhà phát triển JavaScript thử nghiệm trực tiếp với một ứng dụng đang chạy. Ví dụ sau minh họa cách nhập câu lệnh PHP

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
23;

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
8

Tuy nhiên, điều khiến Tinker trở thành một công cụ mạnh mẽ và vô giá là khả năng tương tác với các lớp và thành phần khung, cũng như thực thi mã trong ngữ cảnh của ứng dụng Laravel. Các ví dụ sau giới thiệu cách sử dụng môi trường Tinker REPL để tạo người dùng mới [các dòng bắt đầu bằng

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
24 là đầu vào của người dùng; các dòng bắt đầu bằng
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
25 là đầu ra]

  composer create-project laravel/laravel TinkerTest --prefer-dist
1

Trong ví dụ trước, điều đầu tiên chúng ta làm là tạo một thể hiện

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
26 mới và gán nó cho biến
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
27. Biến
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
27 tồn tại trong suốt phiên Tinker; . Mỗi khi chúng tôi đặt giá trị thuộc tính bằng Tinker, kết quả gán sẽ được hiển thị bên dưới. Tương tự, mỗi khi một phương thức được gọi, giá trị trả về của phương thức sẽ được hiển thị

Ví dụ sau minh họa việc truy xuất một phiên bản

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
26 từ cơ sở dữ liệu bằng Tinker

  composer create-project laravel/laravel TinkerTest --prefer-dist
7

Biểu diễn JSON của mô hình Eloquent

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
26 được hiển thị là kết quả của lệnh gọi phương thức
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
82. Hãy nhớ rằng, theo mặc định, Eloquent models ẩn các trường
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
83 và
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
84 khi chúng được truyền sang JSON. Tuy nhiên, những thứ này có thể dễ dàng được truy cập trực tiếp từ trong phiên Tinker

php artisan migrate
2

Tinker cho phép bạn viết bất kỳ biểu thức mã nào, miễn là nó hợp lệ; . Thật thú vị, REPL đằng sau Tinker cho phép sử dụng câu lệnh

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
85 để nhập các lớp vào phạm vi. Phiên ví dụ sau minh họa điều này bằng cách nhập mô hình
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
26 và tham chiếu nó trong suốt phiên

php artisan migrate
5

#Nhập câu lệnh mã nhiều dòng

Đôi khi, việc nhập mã tùy ý kéo dài nhiều dòng sẽ dễ dàng hơn. Tuy nhiên, theo mặc định, nhấn phím

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
87 sẽ khiến Tinker REPL đọc và đánh giá dòng được nhập. Để giải quyết vấn đề này, hãy kết thúc dòng đầu tiên bằng ký tự
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
88. Điều này cho phép bạn tiếp tục nhập biểu thức trên nhiều dòng; . REPL sẽ tiếp tục chấp nhận đầu vào nhiều dòng cho đến khi gặp phải dấu kết thúc hợp lệ hoặc xảy ra lỗi phân tích cú pháp

Phiên ví dụ minh họa cách tiếp cận này để cung cấp đầu vào cho Tinker REPL

php artisan migrate
9

Trong ví dụ trước, chúng tôi không phải cung cấp ký tự

  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
88. Phiên ví dụ sau sử dụng ký tự
  // see the count of all users
  App\User::count[];

  // find a specific user and see their attributes
  App\User::where['username', 'samuel']->first[];

  // find the relationships of a user
  $user = App\User::with['posts']->first[];
  $user->posts;
88 để buộc REPL vào chế độ nhập nhiều dòng

php artisan tinker
2

#Lệnh Tinker

Giống như ứng dụng dòng lệnh Artisan, Tinker cung cấp nhiều lệnh và tiện ích giúp đơn giản hóa quá trình gỡ lỗi và thực thi mã từ dòng lệnh. Bảng sau đây liệt kê tất cả các lệnh mà Tinker cung cấp [có thể khám phá các lệnh này trong REPL của Tinker bằng cách đưa ra lệnh

  composer create-project laravel/laravel TinkerTest --prefer-dist
12]

CommandDescriptionAlias
  composer create-project laravel/laravel TinkerTest --prefer-dist
12Hiển thị danh sách các lệnh. Nhập
  composer create-project laravel/laravel TinkerTest --prefer-dist
14 để biết thông tin về [foo].
  composer create-project laravel/laravel TinkerTest --prefer-dist
15
  composer create-project laravel/laravel TinkerTest --prefer-dist
16Liệt kê các biến, phương thức và hằng cục bộ, thể hiện hoặc lớp.
  composer create-project laravel/laravel TinkerTest --prefer-dist
17,
  composer create-project laravel/laravel TinkerTest --prefer-dist
18
  composer create-project laravel/laravel TinkerTest --prefer-dist
19Dump một đối tượng hoặc nguyên thủy.
  composer create-project laravel/laravel TinkerTest --prefer-dist
70Đọc tài liệu về đối tượng, lớp, hằng, phương thức hoặc thuộc tính.
  composer create-project laravel/laravel TinkerTest --prefer-dist
71,
  composer create-project laravel/laravel TinkerTest --prefer-dist
72
  composer create-project laravel/laravel TinkerTest --prefer-dist
73Hiển thị mã cho một đối tượng, lớp, hằng, phương thức hoặc thuộc tính.
  composer create-project laravel/laravel TinkerTest --prefer-dist
74Hiển thị dấu vết ngược của ngoại lệ gần đây nhất.
  composer create-project laravel/laravel TinkerTest --prefer-dist
75,
  composer create-project laravel/laravel TinkerTest --prefer-dist
76____277Hiển thị bạn đang ở đâu trong mã.
  composer create-project laravel/laravel TinkerTest --prefer-dist
78Hiển thị ngăn xếp cuộc gọi hiện tại.
  composer create-project laravel/laravel TinkerTest --prefer-dist
79Ném một ngoại lệ ra khỏi Vỏ Psy.
php artisan migrate
20Hiển thị [hoặc xóa] nội dung của bộ đệm nhập mã.
php artisan migrate
21
php artisan migrate
22Xóa màn hình Psy Shell.
php artisan migrate
23Hiển thị lịch sử Psy Shell.
php artisan migrate
24
php artisan migrate
25Kết thúc phiên hiện tại và quay lại người gọi.
php artisan migrate
26,
php artisan migrate
27
php artisan migrate
28Xóa tệp lớp đã biên dịch.
php artisan migrate
29Đặt ứng dụng vào chế độ bảo trì.
php artisan migrate
50Hiển thị môi trường khung hiện tại.
php artisan migrate
51Tối ưu hóa khuôn khổ để có hiệu suất tốt hơn.
php artisan migrate
52Đưa ứng dụng ra khỏi chế độ bảo trì.
php artisan migrate
53Chạy di chuyển cơ sở dữ liệu.
php artisan migrate
54Hiển thị một trích dẫn đầy cảm hứng

Giống như khi tương tác với hầu hết các tiện ích dòng lệnh, mỗi lệnh cho phép bạn xem trợ giúp về bất kỳ lệnh nào. Để làm điều này trong Tinker, chỉ cần đưa ra lệnh

  composer create-project laravel/laravel TinkerTest --prefer-dist
12 [hoặc bất kỳ bí danh nào của nó] theo sau là tên của lệnh mà bạn muốn tìm hiểu thêm về

Làm cách nào để chạy tinker trong Laravel?

Lệnh này được tích hợp trong mọi ứng dụng Laravel và bạn có thể sử dụng nó để chạy mã trong ngữ cảnh ứng dụng của mình. Hãy khám phá điều này có nghĩa là gì. Tinker là một REPL [vòng lặp đọc-eval-in] trên PsySH . Nó nhận đầu vào dòng lệnh của bạn, đánh giá nó và in đầu ra ra bàn điều khiển.

Nghệ nhân trong Laravel là gì?

Artisan là tên của giao diện dòng lệnh có trong Laravel . Nó cung cấp một số lệnh hữu ích để bạn sử dụng trong khi phát triển ứng dụng của mình. Nó được điều khiển bởi thành phần Symfony Console mạnh mẽ.

Tinkerwell là gì?

Tinkerwell là công cụ sửa lại phải có cho các nhà phát triển Laravel và PHP . Nó là trình chạy mã PHP cho Windows, macOS và Linux cho phép bạn chạy mã PHP ngay lập tức.

Chủ Đề