文件路径:appConsoleCommandsUpdateMenu.php

文件作用:

  • 已有菜单汉化
  • 新增菜单操作,支持多级菜单
<?php

namespace App\Console\Commands;

use DB;
use Illuminate\Console\Command;

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

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        $this->info("Ready to Deal Menu");
        $this->line('==========');
        $count = DB::table('admin_menu')->count();
        if ($count == 7) {
            $this->info("Change Default To Chinese");
            DB::table('admin_menu')->where('title', 'Index')->update(['title' => "数据大观"]);
            DB::table('admin_menu')->where('title', 'Admin')->update(['title' => "高级管理", "order" => "199"]);
            DB::table('admin_menu')->where('title', 'Users')->update(['title' => "管理员管理"]);
            DB::table('admin_menu')->where('title', 'Roles')->update(['title' => "角色管理"]);
            DB::table('admin_menu')->where('title', 'Permission')->update(['title' => "权限管理"]);
            DB::table('admin_menu')->where('title', 'Menu')->update(['title' => "菜单管理"]);
            DB::table('admin_menu')->where('title', 'Operation log')->update(['title' => "操作日志"]);
            $this->info("Change Default To Chinese Done!");
        }
        $this->info("Add New Menu");
        $this->line('==========');
        $this->addNewMenu('授课管理', '', 10, 'fa-balance-scale', '');
        $this->addNewMenu('目录管理', '授课管理', 11, 'fa-bars', 'categories');
        $this->addNewMenu('课程管理', '授课管理', 13, 'fa-twitch', 'series');
        $this->addNewMenu('课时管理', '授课管理', 15, 'fa-video-camera', 'episodes');
        $this->info("Add New Menu Done!");

    }
    private function addNewMenu(string $title, $parent_name = '', $order = 90, $icon = 'fa-codepen', $uri = "") {
        if ($parent_name == '') {
            $parent = 0;
        } else {
            $parent_collection = DB::table('admin_menu')->where('title', $parent_name)->select('id')->get();
            if (!count($parent_collection)) {
                $parent = 0;
            } else {
                $parent = $parent_collection[0]->id;
            }
        }

        if (!DB::table('admin_menu')->where('title', $title)->count()) {
            DB::table('admin_menu')->insert([
                "title" => $title,
                "parent_id" => $parent,
                "order" => $order,
                "icon" => $icon,
                'uri' => $uri,
            ]);
        }
    }
}