Laravel学习记录--Laravel创建命令

自定义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 命令名
右击创建的定时任务点击启
-----有时间在来补充

要实现在线用户列表功能,你可以使用 Laravel Echo Server 结合 Laravel Broadcasting 和 Laravel Echo 来实现。下面是实现步骤的概述: 1. 配置 Laravel Broadcasting:首先,确保你的 Laravel 应用程序已经配置了 Broadcasting。你可以使用 Pusher、Redis 或其他支持的广播驱动程序。 2. 安装和配置 Laravel Echo Server:安装并配置 Laravel Echo Server 作为你的实时通信服务器。你可以使用 npm 进行安装,并在 `laravel-echo-server.json` 配置文件中指定你的应用程序密钥、广播驱动程序和其他参数。 3. 创建频道和事件:在 Laravel 应用程序中创建频道和事件,用于处理用户加入和离开频道的逻辑。你可以使用 Laravel 自带的 `php artisan make:channel` 和 `php artisan make:event` 命令创建相应的类。 4. 监听用户加入和离开事件:在频道类中,通过实现 `join` 和 `leave` 方法来监听用户加入和离开频道的事件。在这些方法中,你可以记录在线用户并将信息广播给其他客户端。 5. 前端实现:在前端代码中,使用 Laravel Echo 包来监听频道事件。当有用户加入或离开频道时,接收到事件并更新在线用户列表。 以上是一个简单的概述,实际实现可能会根据你的应用程序和需求有所不同。你可以参考 LaravelLaravel Echo 的官方文档,以及 Laravel Echo Server 的文档来获取更详细的指导和示例代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值