自定义artisan命令
php artisan make:comand CommandName
一,cmd创建命令
创建的命令存放在app\console\commands\下
命令文件
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendPay 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()
{
//命令执行代码
}
}
绑定命令
在kernel文件 设置 $commands属性
$commands = [
\App\Console\Commands\命令名::class
//命令注册到全局
]
二, routes的console.php直接添加命令
<?php
use Illuminate\Foundation\Inspiring;
/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->describe('Display an inspiring quote');
//Artisan::command('命令名',function(){
// 命令执行代码
// })->describe('命令描述')
案例—定时打款
1.创建命令
php artisan make:command SendPay
2.设置命令名/描述/ 并赋予命令执行代码
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class SendPay extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sendpay';
/**
* The console command description.
*
* @var string
*/
protected $description = 'create command sendpay';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{ //自动打款
$paytime = date('Y-m-d:H:i:s',time());
$res = DB::table('tasks;')->where('enddate' ,'>=', $paytime)->get();
// dd($res);
if($res[0]->title){
foreach($res as $v){
$row['uid']=$v->uid;
$row['pid']=$v->pid;
$row['title']=$v->title;
$row['amount']=$v->amount;
$row['paytime']=$paytime;
DB::table('grows')->insert($row);
}
}
}
}
3.绑定命令
app\Console\Kernel.php
protected $commands = [
\App\Console\Commands\SendPay::class;//绑定命令
];
任务计划程序–操作–创建基本任务–启动查询里面
程序或脚本 php 项目路径\artisan 命令名
右击创建的定时任务点击启
-----有时间在来补充