Tạo lệnh tùy chỉnh trong Laravel

Xin chào các bạn, hôm nay mình lại tiếp tục giới thiệu một phần về Laravel 5. 5 - phiên bản mới nhất cho đến thời điểm hiện tại. Artisan Console thực ra không được cải tiến so với các phiên bản trước, nhưng nó cũng là một phần quan trọng luôn được liệt kê trong "Digging Deeper" của Laravel Document. Có thể nhiều bạn mới làm quen với Laravel thì sẽ không hiểu tại sao lại gõ mấy lệnh

php artisan make:command UpdatePublishTime
8 thì hi vọng đọc bài viết này các bạn sẽ hiểu hơn và tự tạo cho mình một lệnh console (nghệ nhân) và chạy thử xem nó có thú vị không

Giới thiệu

Laravel has a command line interface has name is

php artisan make:command UpdatePublishTime
9. Giao diện này cung cấp cho nhà phát triển bạn mình rất nhiều lệnh hữu ích giúp tăng tốc độ phát triển phần mềm. Các bạn có thể xem các lệnh có sẵn mà Artisan cung cấp bằng cách nhập

php artisan list

Mỗi một lệnh thì lại có một màn hình "trợ giúp" để hiển thị và mô tả các đối số (tham số) và các tùy chọn (options) để các bạn dễ dàng thao tác với chúng. Lệnh để mở màn hình trợ giúp này rất đơn giản, chỉ cần thêm



namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
0 vào trước tên lệnh bạn muốn lấy, ví dụ như

php artisan help make:auth

or is



namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
1 (


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
2) Dòng lệnh này mọi người thấy quen thuộc không ạ


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
3. Mọi ứng dụng laravel đều bao gồm tinker, nó là 1 REPL (Read Eval Print Loop), cho phép bạn tương tác với toàn bộ ứng dụng thông qua dòng lệnh, tốt nhất là thực hiện nhiều câu hỏi, công việc, sự kiện,. Hơn nữa, bạn có thể mở rộng nghệ nhân bằng lệnh của riêng bạn tạo ra và tự động hóa một số tác vụ trong ứng dụng của bạn. Nó rất hữu ích

Create and register Command

Tạo lệnh

Laravel giúp bạn làm mọi công việc trở nên dễ dàng hơn, nó đã sẵn sàng hơn lệnh



namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
4 để tạo Artisan Command. Lệnh này sẽ tạo một lớp lệnh đặt trong đường dẫn


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
5. Nếu bạn mở ứng dụng mà không tìm thấy đường dẫn này, thì cũng đừng lo lắng, vì nó sẽ được tạo ra lần đầu tiên khi bạn chạy lệnh


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
6. Ví dụ dưới đây

php artisan make:command UpdatePublishTime



namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
7 main is name of command class. Mục đích ở ví dụ này của mình chỉ đơn giản là cập nhật cột


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
8 trong bảng


namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}
9 của mình

Cấu trúc lớp lệnh

Sau khi chạy lệnh trên, các bạn mở tệp

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
0 ra các bạn sẽ thấy cấu trúc của một lớp lệnh như sau



namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

Sau khi tạo xong như thế này rồi thì các bạn hãy điền thông tin vào các thuộc tính

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
1 và
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
2 của lớp, nó 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 Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
3. Còn hàm
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
4 sẽ được gọi mỗi khi lệnh của bạn được thực thi. Thế nên có xử lý logic gì thì cứ cho hết vào hàm này là oke

Xử lý logic trong lớp lệnh

Ở đây mình sẽ sửa lại lớp của mình như sau

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}

Ở đoạn mã trên, sau khi cập nhật thành công mình sẽ đưa ra các thông báo thông báo trên giao diện lệnh như là.

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
5,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
6,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
7,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
8,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
9. Mỗi một phương thức sẽ sử dụng các màu tương ứng với mục đích của chúng, ví dụ như
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
6 sẽ ra chữ xanh,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
8 là chữ đỏ,
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
5 thì không xét màu. Hoặc là bạn cũng có thể đưa ra một bảng chứa nội dung mà bạn vừa cập nhật như sau

$headers = ['Name', 'Publish Time'];
$posts = App\Post::all(['name', 'publish_time'])->toArray();

$this->table($headers, $posts);

Ngoài ra, các bạn còn có thể đưa ra giao diện lệnh nhắc nhở cho đầu vào như một câu hỏi

// Stop execution and ask a question 
$answer = $this->ask('What is your name?');

Câu hỏi mà người dùng không nhìn thấy nội dung mình nhập

// Ask for sensitive information
$password = $this->secret('What is the password?');

Câu hỏi lựa chọn

// Choices
$name = $this->choice('What is your name?', ['Trang', 'Boo'], $default);

Câu hỏi cần xác nhận có/không

// Confirmation
if ($this->confirm('Is '.$name.' correct, do you wish to continue? [y|N]')) {
//
}

Đối số và Tùy chọn

Tiếp tục ví dụ trên, nếu như sau này bạn lại muốn cập nhật cột dữ liệu đó về một giá trị khác thì bạn phải làm thế nào? . Trong Laravel, thuộc tính

$headers = ['Name', 'Publish Time'];
$posts = App\Post::all(['name', 'publish_time'])->toArray();

$this->table($headers, $posts);
3 là nơi để định nghĩa các đối số và tùy chọn, như dưới đây

php artisan help make:auth
0

Đối số được đăng ký một cách dễ dàng với cú pháp {argumentName}, còn tùy chọn thì là cú pháp {--optionName}. Chúng ta có thể tạo ra nhiều đối số và tùy chọn khác nhau và không cần phải tạo một hàm nhận đối số hoặc nhận tùy chọn trong lớp mà chỉ cần gọi nó trong hàm

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class UpdatePublishTime extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'posts:update_publish_time';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Update publish time column';

    //**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            DB::beginTransaction();
          
            //update publish_time column
            DB::table('posts')->update([
                'publish_time' => '2020-12-31 00:00:00',
            ]);
            
            DB::commit();
           
            //send output to the console
            $this->info('Success!');
        } catch (\Exception $e) {
            DB::rollBack();
            
            $this->error($e->getMessage());
        }
    }
}
4 như sau

php artisan help make:auth
1

Đó là những cú pháp đơn giản nhất để tạo đối số và tùy chọn. Ngoài ra các bạn còn có thêm các cách sử dụng

  • Tranh luận.
    $headers = ['Name', 'Publish Time'];
    $posts = App\Post::all(['name', 'publish_time'])->toArray();
    
    $this->table($headers, $posts);
    
    5
  • Đối số tùy chọn.
    $headers = ['Name', 'Publish Time'];
    $posts = App\Post::all(['name', 'publish_time'])->toArray();
    
    $this->table($headers, $posts);
    
    6
  • Đối số có giá trị mặc định.
    $headers = ['Name', 'Publish Time'];
    $posts = App\Post::all(['name', 'publish_time'])->toArray();
    
    $this->table($headers, $posts);
    
    7
  • Tùy chọn Boolean.
    $headers = ['Name', 'Publish Time'];
    $posts = App\Post::all(['name', 'publish_time'])->toArray();
    
    $this->table($headers, $posts);
    
    8
  • Tùy chọn có giá trị.
    $headers = ['Name', 'Publish Time'];
    $posts = App\Post::all(['name', 'publish_time'])->toArray();
    
    $this->table($headers, $posts);
    
    9
  • Tùy chọn có Giá trị và Mặc định.
    // Stop execution and ask a question 
    $answer = $this->ask('What is your name?');
    
    0

Đăng ký lệnh

Đăng ký lệnh vừa tạo bằng cách thêm tên lớp vào thuộc tính

// Stop execution and ask a question 
$answer = $this->ask('What is your name?');
1 trong
// Stop execution and ask a question 
$answer = $this->ask('What is your name?');
2. 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 các thùng chứa dịch vụ và đăng ký với Artisan

Làm cách nào để tạo các lệnh console trong Laravel?

Dưới đây là các bước để tạo một lệnh thủ công mới. .
Bước 1. Tạo một ứng dụng Laravel mới. laravel tùy chỉnh mới
Bước 2. Tạo một lệnh. sử dụng làm cho. command command để tạo một command mới. Chỉ cần nhập tên lệnh, như vậy. nghệ nhân php làm. lệnh CheckUsers

Làm cách nào để tạo bộ điều khiển mới trong Laravel?

Tạo Bộ điều khiển . Điều này sẽ tạo ra một hàm tạo đơn giản khi chúng ta truyền đối số — đơn giản. Nếu bạn không muốn tạo một hàm tạo đơn giản, bạn chỉ cần bỏ qua đối số. Hàm tạo đã tạo có thể được nhìn thấy tại app/Http/Controllers. Replace the with the name of your controller. This will create a plain constructor as we are passing the argument — plain. If you don't want to create a plain constructor, you can simply ignore the argument. The created constructor can be seen at app/Http/Controllers.

Lệnh artisan 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ẽ.

Làm cách nào để chạy dự án Laravel bằng lệnh artisan?

laravellocal. md .
Tạo một cơ sở dữ liệu có tên cục bộ là homestead utf8_General_ci
Kéo dự án Laravel/php từ nhà cung cấp git
Đổi tên. .
Mở bàn điều khiển và cd thư mục gốc dự án của bạn
Chạy cài đặt trình soạn thảo hoặc trình soạn thảo php. .
Chạy khóa thủ công php. phát ra
Chạy php artisan di chuyển
Chạy php artisan db. hạt giống để chạy máy gieo hạt, nếu có