Người dùng cập nhật Php artisan tinker

Trong hướng dẫn Laravel này, tôi sẽ giải thích cho bạn về 'Tinker', một trong những tính năng tuyệt vời trong ứng dụng Laravel cho phép người dùng tương tác với toàn bộ ứng dụng Laravel từ dòng lệnh

Bạn có thể đặt tất cả các truy vấn hùng hồn trên dòng lệnh với sự trợ giúp của Tinker

Với sự trợ giúp của các tính năng ít được biết đến của Laravel, Bạn có thể nhanh chóng đọc dữ liệu từ Cơ sở dữ liệu trong ứng dụng Laravel

Laravel tinker là một bản thay thế (Đọc–Eval–Vòng in) được cung cấp bởi gói PsySH

Trước khi mày mò cài ứng dụng Laravel rồi chạy lệnh migration để tạo table

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

94

Sau khi chạy lệnh di chuyển, bạn sẽ thấy đầu ra sau
laravel tinker

Bây giờ hãy chạy lệnh artisan để vào môi trường tinker.
______195

Cơ sở dữ liệu Seeding với người dùng giả

Đầu tiên, chúng tôi sẽ tạo cơ sở dữ liệu của mình với 10 chi tiết người dùng mới bằng cách chạy dòng lệnh sau

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

96

Bạn có thể đếm tổng số người dùng trong cơ sở dữ liệu bằng cách chạy lệnh sau.
______197

Thêm người dùng mới

Bạn có thể tạo người dùng từ thay thế. Tôi đã nói với bạn rằng bạn có thể đặt các truy vấn hùng hồn của mình giống như bạn viết mã trong ứng dụng Laravel

Tinker cho phép bạn tương tác với toàn bộ ứng dụng Laravel của mình trên dòng lệnh, bao gồm Eloquent ORM, công việc, sự kiện, v.v. Để vào môi trường Tinker, hãy chạy lệnh tinker Artisan

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

98

bằng cách sử dụng dòng lệnh tinker, chúng ta có thể tạo người dùng mới hoặc chèn dữ liệu mới vào cơ sở dữ liệu

chạy

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

99

hoặc bạn có thể sử dụng các phương pháp khác nhau để lưu trữ dữ liệu mới, hãy xem

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

99

Artisan là giao diện dòng lệnh đi kèm với Laravel. Artisan tồn tại ở thư mục gốc của ứng dụng của bạn dưới dạng tập lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

3 và cung cấp một số lệnh hữu ích có thể hỗ trợ bạn trong khi xây dựng ứng dụng của mình. Để xem danh sách tất cả các lệnh Artisan có sẵn, bạn có thể sử dụng lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

4

Mỗi lệnh cũng bao gồm một màn hình "trợ giúp" hiển thị và mô tả các đối số và tùy chọn có sẵn của lệnh. Để xem màn hình trợ giúp, hãy đặt trước tên của lệnh bằng

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

5

Cánh buồm Laravel

Nếu bạn đang sử dụng Laravel Sail làm môi trường phát triển cục bộ của mình, hãy nhớ sử dụng dòng lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

6 để gọi các lệnh Artisan. Sail sẽ thực thi các lệnh Artisan của bạn trong bộ chứa Docker của ứng dụng của bạn

./vendor/bin/sail artisan list

Tinker (REPL)

Laravel Tinker là một REPL mạnh mẽ cho Laravel framework, được hỗ trợ bởi gói PsySH

Cài đặt

Tất cả các ứng dụng Laravel đều bao gồm Tinker theo mặc định. Tuy nhiên, bạn có thể cài đặt Tinker bằng Composer nếu trước đó bạn đã xóa nó khỏi ứng dụng của mình

composer require laravel/tinker

Lưu ý
Bạn đang tìm giao diện người dùng đồ họa để tương tác với ứng dụng Laravel của mình? .

Cách sử dụng

Tinker cho phép bạn tương tác với toàn bộ ứng dụng Laravel của mình trên dòng lệnh, bao gồm các mô hình Eloquent, công việc, sự kiện, v.v. Để vào môi trường Tinker, hãy chạy lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

7 Artisan

Bạn có thể xuất bản tệp cấu hình của Tinker bằng lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

8

________số 8

Cảnh báo
Hàm trợ giúp

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

9 và phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

9 trên lớp

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

11 phụ thuộc vào bộ sưu tập rác để đặt công việc vào hàng đợi. Vì vậy, khi sử dụng tinker, bạn nên sử dụng

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

12 hoặc

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

13 để điều phối công việc.

Danh sách cho phép lệnh

Tinker sử dụng danh sách "cho phép" để xác định lệnh Artisan nào được phép chạy trong trình bao của nó. Theo mặc định, bạn có thể chạy các lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

14,

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

15,

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

16,

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

17,

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

18,

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

19 và

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

00. Nếu bạn muốn cho phép nhiều lệnh hơn, bạn có thể thêm chúng vào mảng

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

01 trong tệp cấu hình

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

02 của mình

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

3

Các lớp không nên đặt bí danh

Thông thường, Tinker tự động đặt bí danh cho các lớp khi bạn tương tác với chúng trong Tinker. Tuy nhiên, bạn có thể muốn không bao giờ bí danh một số lớp. Bạn có thể thực hiện điều này bằng cách liệt kê các lớp trong mảng

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

03 của tệp cấu hình

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

02 của bạn

viết lệnh

Ngoài các lệnh được cung cấp bởi Artisan, bạn có thể tạo các lệnh tùy chỉnh của riêng mình. Các lệnh thường được lưu trữ trong thư mục

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

05;

Tạo lệnh

Để tạo một lệnh mới, bạn có thể sử dụng lệnh Artisan

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

06. Lệnh này sẽ tạo một lớp lệnh mới trong thư mục

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

05. Đừng lo lắng nếu thư mục này không tồn tại trong ứng dụng của bạn - nó sẽ được tạo lần đầu tiên khi bạn chạy lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

06 Artisan

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

0

Cấu trúc lệnh

Sau khi tạo lệnh của bạn, bạn nên xác định các giá trị thích hợp cho các thuộc tính

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

09 và

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

20 của lớp. Các thuộc tính này sẽ được sử dụng khi hiển thị lệnh của bạn trên màn hình

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

4. Thuộc tính

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

09 cũng cho phép bạn xác định kỳ vọng đầu vào của lệnh. Phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

23 sẽ được gọi khi lệnh của bạn được thực thi. Bạn có thể đặt logic lệnh của mình trong phương thức này

Hãy xem một lệnh ví dụ. Lưu ý rằng chúng tôi có thể yêu cầu bất kỳ phụ thuộc nào chúng tôi cần thông qua phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

23 của lệnh. Bộ chứa dịch vụ Laravel sẽ tự động thêm tất cả các phụ thuộc được gợi ý kiểu trong chữ ký của phương thức này

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

Lưu ý
Để tái sử dụng mã hiệu quả hơn, bạn nên giữ cho các lệnh trên bảng điều khiển của mình nhẹ nhàng và để chúng chuyển sang các dịch vụ ứng dụng để hoàn thành nhiệm vụ của chúng. Trong ví dụ trên, lưu ý rằng chúng tôi đưa vào một lớp dịch vụ để thực hiện "công việc nặng nhọc" gửi e-mail.

Lệnh đóng cửa

Các lệnh dựa trên đóng cửa cung cấp một giải pháp thay thế để xác định các lệnh điều khiển dưới dạng các lớp. Cũng giống như cách đóng tuyến đường là một giải pháp thay thế cho bộ điều khiển, hãy nghĩ đến việc đóng lệnh như một giải pháp thay thế cho các lớp lệnh. Trong phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

01 của tệp

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

26 của bạn, Laravel tải tệp

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

27

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

1

Mặc dù tệp này không xác định các tuyến HTTP, nhưng nó xác định các điểm vào dựa trên bảng điều khiển (các tuyến) vào ứng dụng của bạn. Trong tệp này, bạn có thể xác định tất cả các lệnh bảng điều khiển dựa trên đóng của mình bằng cách sử dụng phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

28. Phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

29 chấp nhận hai đối số. chữ ký lệnh và bao đóng nhận các đối số và tùy chọn của lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

0

Bao đóng được liên kết với thể hiện lệnh cơ bản, vì vậy bạn có toàn quyền truy cập vào tất cả các phương thức của trình trợ giúp mà bạn thường có thể truy cập trên một lớp lệnh đầy đủ

Phụ thuộc gợi ý kiểu

Ngoài việc nhận các đối số và tùy chọn của lệnh của bạn, các lần đóng lệnh cũng có thể gợi ý loại phụ thuộc bổ sung mà bạn muốn giải quyết ngoài vùng chứa dịch vụ

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

2

Mô tả lệnh đóng cửa

Khi xác định lệnh dựa trên đóng, bạn có thể sử dụng phương thức

./vendor/bin/sail artisan list

60 để thêm mô tả cho lệnh. Mô tả này sẽ được hiển thị khi bạn chạy lệnh

./vendor/bin/sail artisan list

61 hoặc

./vendor/bin/sail artisan list

62

./vendor/bin/sail artisan list

6

Các lệnh cô lập

Cảnh báo Để sử dụng tính năng này, ứng dụng của bạn phải đang sử dụng trình điều khiển bộ đệm ẩn

./vendor/bin/sail artisan list

63,

./vendor/bin/sail artisan list

64,

./vendor/bin/sail artisan list

65,

./vendor/bin/sail artisan list

66,

./vendor/bin/sail artisan list

67 hoặc

./vendor/bin/sail artisan list

68 làm trình điều khiển bộ đệm mặc định cho ứng dụng của bạn. Ngoài ra, tất cả các máy chủ phải giao tiếp với cùng một máy chủ bộ đệm trung tâm

Đôi khi bạn có thể muốn đảm bảo rằng chỉ có một phiên bản của lệnh có thể chạy tại một thời điểm. Để thực hiện điều này, bạn có thể triển khai giao diện

./vendor/bin/sail artisan list

69 trên lớp lệnh của mình

composer require laravel/tinker

0

Khi một lệnh được đánh dấu là

composer require laravel/tinker

00, Laravel sẽ tự động thêm tùy chọn

composer require laravel/tinker

01 vào lệnh. Khi lệnh được gọi với tùy chọn đó, Laravel sẽ đảm bảo rằng không có phiên bản nào khác của lệnh đó đang chạy. Laravel thực hiện điều này bằng cách cố gắng lấy khóa nguyên tử bằng trình điều khiển bộ đệm mặc định của ứng dụng của bạn. Nếu các phiên bản khác của lệnh đang chạy, lệnh sẽ không thực thi;

composer require laravel/tinker

1

Nếu bạn muốn chỉ định mã trạng thái thoát mà lệnh sẽ trả về nếu nó không thể thực thi, bạn có thể cung cấp mã trạng thái mong muốn thông qua tùy chọn

composer require laravel/tinker

02

composer require laravel/tinker

2

Thời gian hết hạn khóa

Theo mặc định, khóa cách ly hết hạn sau khi lệnh kết thúc. Hoặc, nếu lệnh bị gián đoạn và không thể hoàn thành, khóa sẽ hết hạn sau một giờ. Tuy nhiên, bạn có thể điều chỉnh thời gian hết hạn khóa bằng cách xác định phương thức

composer require laravel/tinker

03 trong lệnh của mình

composer require laravel/tinker

3

Xác định kỳ vọng đầu vào

Khi viết các lệnh trên bàn điều khiển, thông thường sẽ thu thập thông tin đầu vào từ người dùng thông qua các đối số hoặc tùy chọn. Laravel làm cho việc xác định đầu vào mà bạn mong đợi từ người dùng trở nên rất thuận tiện bằng cách sử dụng thuộc tính

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

09 trên các lệnh của bạn. Thuộc tính

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

09 cho phép bạn xác định tên, đối số và các tùy chọn cho lệnh theo một cú pháp đơn, biểu cảm, giống như tuyến đường

Tranh luận

Tất cả các đối số và tùy chọn do người dùng cung cấp được đặt trong dấu ngoặc nhọn. Trong ví dụ sau, lệnh xác định một đối số bắt buộc.

composer require laravel/tinker

06

composer require laravel/tinker

4

Bạn cũng có thể đặt đối số tùy chọn hoặc xác định giá trị mặc định cho đối số

composer require laravel/tinker

5

Tùy chọn

Các tùy chọn, như đối số, là một dạng đầu vào khác của người dùng. Các tùy chọn có tiền tố là hai dấu gạch ngang (_______507) khi chúng được cung cấp qua dòng lệnh. Có hai loại tùy chọn. những cái nhận được một giá trị và những cái không. Các tùy chọn không nhận được giá trị đóng vai trò là "công tắc" boolean. Chúng ta hãy xem một ví dụ về loại tùy chọn này

composer require laravel/tinker

6

Trong ví dụ này, công tắc

composer require laravel/tinker

08 có thể được chỉ định khi gọi lệnh Artisan. Nếu công tắc

composer require laravel/tinker

08 được thông qua, giá trị của tùy chọn sẽ là

composer require laravel/tinker

10. Nếu không, giá trị sẽ là

composer require laravel/tinker

11

composer require laravel/tinker

7

Tùy chọn có giá trị

Tiếp theo, chúng ta hãy xem một tùy chọn mong đợi một giá trị. Nếu người dùng phải chỉ định một giá trị cho một tùy chọn, bạn nên thêm dấu

composer require laravel/tinker

12 vào tên tùy chọn đó

composer require laravel/tinker

8

Trong ví dụ này, người dùng có thể chuyển một giá trị cho tùy chọn như vậy. Nếu tùy chọn không được chỉ định khi gọi lệnh, giá trị của nó sẽ là

composer require laravel/tinker

13

composer require laravel/tinker

9

Bạn có thể gán giá trị mặc định cho tùy chọn bằng cách chỉ định giá trị mặc định sau tên tùy chọn. Nếu người dùng không chuyển giá trị tùy chọn, giá trị mặc định sẽ được sử dụng

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

0

Phím tắt tùy chọn

Để gán một lối tắt khi xác định một tùy chọn, bạn có thể chỉ định nó trước tên tùy chọn và sử dụng ký tự

composer require laravel/tinker

14 làm dấu phân cách để tách lối tắt khỏi tên tùy chọn đầy đủ

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

1

Khi gọi lệnh trên thiết bị đầu cuối của bạn, các phím tắt tùy chọn phải được bắt đầu bằng một dấu gạch nối

Mảng đầu vào

Nếu bạn muốn xác định các đối số hoặc tùy chọn để mong đợi nhiều giá trị đầu vào, bạn có thể sử dụng ký tự

composer require laravel/tinker

15. Đầu tiên, chúng ta hãy xem một ví dụ xác định một đối số như vậy

Khi gọi phương thức này, các đối số

composer require laravel/tinker

06 có thể được chuyển theo thứ tự tới dòng lệnh. Ví dụ: lệnh sau sẽ đặt giá trị của

composer require laravel/tinker

06 thành một mảng có giá trị là

composer require laravel/tinker

18 và

composer require laravel/tinker

19

Ký tự

composer require laravel/tinker

15 này có thể được kết hợp với một định nghĩa đối số tùy chọn để cho phép không hoặc nhiều phiên bản của đối số

Mảng tùy chọn

Khi xác định một tùy chọn mong đợi nhiều giá trị đầu vào, mỗi giá trị tùy chọn được chuyển đến lệnh phải được thêm tiền tố vào tên tùy chọn

Một lệnh như vậy có thể được gọi bằng cách chuyển nhiều đối số

composer require laravel/tinker

21

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

2

Mô tả đầu vào

Bạn có thể gán mô tả cho các đối số và tùy chọn đầu vào bằng cách tách tên đối số khỏi mô tả bằng dấu hai chấm. Nếu bạn cần thêm một chút chỗ để xác định lệnh của mình, vui lòng trải rộng định nghĩa trên nhiều dòng

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

3

Lệnh vào/ra

Truy xuất đầu vào

Trong khi lệnh của bạn đang thực thi, bạn có thể sẽ cần truy cập các giá trị cho các đối số và tùy chọn được lệnh của bạn chấp nhận. Để làm như vậy, bạn có thể sử dụng các phương pháp

composer require laravel/tinker

22 và

composer require laravel/tinker

23. Nếu một đối số hoặc tùy chọn không tồn tại,

composer require laravel/tinker

13 sẽ được trả về

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

4

Nếu bạn cần truy xuất tất cả các đối số dưới dạng

./vendor/bin/sail artisan list

68, hãy gọi phương thức

composer require laravel/tinker

26

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

5

Các tùy chọn có thể được truy xuất dễ dàng như các đối số bằng phương thức

composer require laravel/tinker

23. Để truy xuất tất cả các tùy chọn dưới dạng một mảng, hãy gọi phương thức

composer require laravel/tinker

28

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

6

Nhắc nhập liệu

Ngoài việc hiển thị đầu ra, bạn cũng có thể yêu cầu người dùng cung cấp thông tin đầu vào trong quá trình thực thi lệnh của bạn. Phương thức

composer require laravel/tinker

29 sẽ nhắc người dùng với câu hỏi đã cho, chấp nhận đầu vào của họ và sau đó trả lại đầu vào của người dùng cho lệnh của bạn

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

7

Phương pháp

composer require laravel/tinker

30 tương tự như

composer require laravel/tinker

29, nhưng đầu vào của người dùng sẽ không hiển thị với họ khi họ nhập vào bảng điều khiển. Phương pháp này hữu ích khi yêu cầu thông tin nhạy cảm như mật khẩu

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

8

Yêu cầu xác nhận

Nếu bạn cần yêu cầu người dùng xác nhận đơn giản "có hoặc không", bạn có thể sử dụng phương pháp

composer require laravel/tinker

32. Theo mặc định, phương thức này sẽ trả về

composer require laravel/tinker

11. Tuy nhiên, nếu người dùng nhập

composer require laravel/tinker

34 hoặc

composer require laravel/tinker

35 để phản hồi lời nhắc, phương thức sẽ trả về

composer require laravel/tinker

10

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

9

Nếu cần, bạn có thể chỉ định rằng lời nhắc xác nhận sẽ trả về

composer require laravel/tinker

10 theo mặc định bằng cách chuyển

composer require laravel/tinker

10 làm đối số thứ hai cho phương thức

composer require laravel/tinker

32

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

30

Tự động hoàn thành

Phương pháp

composer require laravel/tinker

40 có thể được sử dụng để cung cấp tính năng tự động hoàn thành cho các lựa chọn có thể. Người dùng vẫn có thể cung cấp bất kỳ câu trả lời nào, bất kể gợi ý tự động hoàn thành

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

31

Ngoài ra, bạn có thể chuyển một bao đóng làm đối số thứ hai cho phương thức

composer require laravel/tinker

40. Việc đóng sẽ được gọi mỗi khi người dùng nhập một ký tự đầu vào. Việc đóng cửa phải chấp nhận một tham số chuỗi chứa đầu vào của người dùng cho đến nay và trả về một loạt các tùy chọn để tự động hoàn thành

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

32

Câu hỏi trắc nghiệm

Nếu bạn cần cung cấp cho người dùng một tập hợp các lựa chọn được xác định trước khi đặt câu hỏi, bạn có thể sử dụng phương pháp

composer require laravel/tinker

42. Bạn có thể đặt chỉ mục mảng của giá trị mặc định được trả về nếu không có tùy chọn nào được chọn bằng cách chuyển chỉ mục làm đối số thứ ba cho phương thức

Ngoài ra, phương pháp

composer require laravel/tinker

42 chấp nhận các đối số thứ tư và thứ năm tùy chọn để xác định số lần thử tối đa để chọn một phản hồi hợp lệ và liệu có cho phép nhiều lựa chọn hay không

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

33

Viết đầu ra

Để gửi đầu ra tới bàn điều khiển, bạn có thể sử dụng các phương thức

composer require laravel/tinker

44,

composer require laravel/tinker

45,

composer require laravel/tinker

46,

composer require laravel/tinker

47,

composer require laravel/tinker

48 và

composer require laravel/tinker

49. Mỗi phương pháp này sẽ sử dụng các màu ANSI thích hợp cho mục đích của chúng. Ví dụ: hãy hiển thị một số thông tin chung cho người dùng. Thông thường, phương pháp

composer require laravel/tinker

45 sẽ hiển thị trong bảng điều khiển dưới dạng văn bản màu xanh lục

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

34

Để hiển thị thông báo lỗi, hãy sử dụng phương pháp

composer require laravel/tinker

49. Văn bản thông báo lỗi thường được hiển thị bằng màu đỏ

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

35

Bạn có thể sử dụng phương pháp

composer require laravel/tinker

44 để hiển thị văn bản đơn giản, không màu

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

36

Bạn có thể sử dụng phương pháp

composer require laravel/tinker

53 để hiển thị một dòng trống

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

37

Những cái bàn

Phương thức

composer require laravel/tinker

54 giúp dễ dàng định dạng chính xác nhiều hàng/cột dữ liệu. Tất cả những gì bạn cần làm là cung cấp tên cột và dữ liệu cho bảng và Laravel sẽ tự động tính toán chiều rộng và chiều cao phù hợp của bảng cho bạn

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

38

Thanh tiến trình

Đối với các tác vụ chạy trong thời gian dài, có thể hữu ích khi hiển thị thanh tiến trình thông báo cho người dùng mức độ hoàn thành của tác vụ. Sử dụng phương thức

composer require laravel/tinker

55, Laravel sẽ hiển thị một thanh tiến trình và nâng cao tiến trình của nó cho mỗi lần lặp qua một giá trị có thể lặp lại nhất định

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

39

Đôi khi, bạn có thể cần kiểm soát thủ công hơn đối với cách nâng cao thanh tiến trình. Đầu tiên, xác định tổng số bước mà quy trình sẽ lặp lại. Sau đó, chuyển tiếp thanh tiến trình sau khi xử lý từng mục

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

00

Lưu ý
Để biết thêm các tùy chọn nâng cao, hãy xem tài liệu thành phần Thanh tiến trình Symfony.

Đăng ký lệnh

Tất cả các lệnh bảng điều khiển của bạn được đăng ký trong lớp

composer require laravel/tinker

56 của ứng dụng, là "nhân bảng điều khiển" của ứng dụng của bạn. Trong phương thức

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

01 của lớp này, bạn sẽ thấy một cuộc gọi đến phương thức

composer require laravel/tinker

58 của kernel. Phương thức

composer require laravel/tinker

58 sẽ quét thư mục

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

05 và tự động đăng ký từng lệnh trong đó với Artisan. Bạn thậm chí có thể tự do gọi thêm phương thức

composer require laravel/tinker

58 để quét các thư mục khác để tìm lệnh Artisan

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

01

Nếu cần, bạn có thể đăng ký các lệnh theo cách thủ công bằng cách thêm tên lớp của lệnh vào thuộc tính

composer require laravel/tinker

62 trong lớp

composer require laravel/tinker

56 của bạn. Nếu thuộc tính này chưa được xác định trên kernel của bạn, bạn nên xác định nó theo cách thủ công. Khi Artisan khởi động, tất cả các lệnh được liệt kê trong thuộc tính này sẽ được giải quyết bởi bộ chứa dịch vụ và được đăng ký với Artisan

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

02

Các lệnh thực thi theo chương trình

Đôi khi bạn có thể muốn thực thi lệnh Artisan bên ngoài CLI. Ví dụ: bạn có thể muốn thực thi lệnh Artisan từ tuyến đường hoặc bộ điều khiển. Bạn có thể sử dụng phương pháp

composer require laravel/tinker

64 trên mặt tiền

composer require laravel/tinker

65 để thực hiện việc này. Phương thức

composer require laravel/tinker

64 chấp nhận tên chữ ký của lệnh hoặc tên lớp làm đối số đầu tiên và một mảng tham số lệnh làm đối số thứ hai. Mã thoát sẽ được trả lại

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

03

Ngoài ra, bạn có thể truyền toàn bộ lệnh Artisan cho phương thức

composer require laravel/tinker

64 dưới dạng chuỗi

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

04

Truyền giá trị mảng

Nếu lệnh của bạn xác định một tùy chọn chấp nhận một mảng, bạn có thể chuyển một mảng giá trị cho tùy chọn đó

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

05

Truyền giá trị Boolean

Nếu bạn cần chỉ định giá trị của một tùy chọn không chấp nhận giá trị chuỗi, chẳng hạn như cờ

composer require laravel/tinker

68 trên lệnh

composer require laravel/tinker

69, bạn nên chuyển

composer require laravel/tinker

10 hoặc

composer require laravel/tinker

11 làm giá trị của tùy chọn

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

06

Lệnh thủ công xếp hàng

Sử dụng phương pháp

composer require laravel/tinker

72 trên mặt tiền

composer require laravel/tinker

65, bạn thậm chí có thể xếp hàng các lệnh Artisan để chúng được xử lý trong nền bởi nhân viên xếp hàng của bạn. Trước khi sử dụng phương pháp này, hãy đảm bảo rằng bạn đã định cấu hình hàng đợi của mình và đang chạy trình xử lý hàng đợi

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

07

Sử dụng các phương thức

composer require laravel/tinker

74 và

composer require laravel/tinker

75, bạn có thể chỉ định kết nối hoặc hàng đợi lệnh Artisan sẽ được gửi tới

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

08

Gọi lệnh từ các lệnh khác

Đôi khi bạn có thể muốn gọi các lệnh khác từ lệnh Artisan hiện có. Bạn có thể làm như vậy bằng cách sử dụng phương pháp

composer require laravel/tinker

64. Phương thức

composer require laravel/tinker

64 này chấp nhận tên lệnh và một mảng các đối số/tùy chọn của lệnh

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

09

Nếu bạn muốn gọi một lệnh console khác và chặn tất cả đầu ra của nó, bạn có thể sử dụng phương thức

composer require laravel/tinker

78. Phương thức

composer require laravel/tinker

78 có cùng chữ ký với phương thức

composer require laravel/tinker

64

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

0

Xử lý tín hiệu

Như bạn có thể biết, hệ điều hành cho phép gửi tín hiệu đến các tiến trình đang chạy. Ví dụ: tín hiệu

composer require laravel/tinker

81 là cách hệ điều hành yêu cầu chương trình chấm dứt. Nếu bạn muốn lắng nghe các tín hiệu trong các lệnh của bảng điều khiển Artisan và thực thi mã khi chúng xuất hiện, bạn có thể sử dụng phương pháp

composer require laravel/tinker

82

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

1

Để lắng nghe nhiều tín hiệu cùng một lúc, bạn có thể cung cấp một mảng tín hiệu cho phương pháp

composer require laravel/tinker

82

namespace App\Console\Commands;

use App\Support\DripEmailer;

use Illuminate\Console\Command;

class SendEmails extends Command

* The name and signature of the console command.

protected $signature = 'mail:send {user}';

* The console command description.

protected $description = 'Send a marketing email to a user';

* Execute the console command.

* @param \App\Support\DripEmailer $drip

public function handle(DripEmailer $drip)

$drip->send(User::find($this->argument('user')));

2

Tùy chỉnh sơ khai

Các lệnh

composer require laravel/tinker

84 của bảng điều khiển Artisan được sử dụng để tạo nhiều lớp khác nhau, chẳng hạn như bộ điều khiển, công việc, di chuyển và thử nghiệm. Các lớp này được tạo bằng các tệp "sơ khai" được điền các giá trị dựa trên thông tin đầu vào của bạn. Tuy nhiên, bạn có thể muốn thực hiện các thay đổi nhỏ đối với các tệp do Artisan tạo. Để thực hiện điều này, bạn có thể sử dụng lệnh

composer require laravel/tinker

85 để xuất bản các sơ khai phổ biến nhất cho ứng dụng của mình để bạn có thể tùy chỉnh chúng.

Các sơ khai đã xuất bản sẽ nằm trong thư mục

composer require laravel/tinker

86 trong thư mục gốc của ứng dụng của bạn. Bất kỳ thay đổi nào bạn thực hiện đối với các sơ khai này sẽ được phản ánh khi bạn tạo các lớp tương ứng của chúng bằng cách sử dụng các lệnh Artisan's

composer require laravel/tinker

84

Sự kiện

Artisan gửi ba sự kiện khi chạy lệnh.

composer require laravel/tinker

88,

composer require laravel/tinker

89 và

composer require laravel/tinker

90. Sự kiện

composer require laravel/tinker

91 được gửi đi ngay lập tức khi Artisan bắt đầu chạy. Tiếp theo, sự kiện

composer require laravel/tinker

92 được gửi ngay trước khi lệnh chạy. Cuối cùng, sự kiện

composer require laravel/tinker

93 được gửi đi sau khi lệnh thực thi xong