侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

laravel-admin 添加Excel导入功能

Laravel  /  管理员 发布于 2年前   4520
  1. 引入laravel-excel;

  2. 添加导入按钮:

 //导入
 $grid->tools(function ($tools) {
   $tools->append(new ExcelImport());
 });
 //导出
 $grid->exporter(new ExcelExporter('cardnews'));

   image.png

   3. 导入按钮控制器 

<?php
namespace App\Admin\Extensions\Tools;
use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;
use Illuminate\Support\Facades\Request;

class ExcelImport extends AbstractTool
{
   public function script()
   {
       return <<<EOT
   $('.file-upload').on('change', function () {
       $('.file-upload-form').submit();
   });
EOT;
   }
   public function render()
   {
       Admin::script($this->script());
       if (Request::path() == 'loansarticles'){
           $url = 'loansnewsimport';
       }else {
           $url = 'cardnewsimport';
       }
       return view('admin.tools.excelimport')->with('url',$url);
   }
}

  4. 按钮视图:\resources\views\admin\tools\excelimport.blade.php

<label class="btn btn-success"{{-- data-toggle="modal" data-target="#uploadModal"--}}>
   <i class="fa fa-stack-overflow"></i>&nbsp;&nbsp;数据导入
   <form action="{{$url}}" method="post" class="file-upload-form" enctype="multipart/form-data" pjax-container>
       <input type="file" name="files[]" class="hidden file-upload" multiple>
       {{ csrf_field() }}
   </form>
</label>

  5. 导入的方法:重组数据拼sql,插入数据库

public function import(Request $request)
 {
     $files = $request->file('files');
     $dir = $request->get('dir', '/');
     $manager = new MediaManager($dir,'xlsx');
     try {
         //文件上传服务器
         if ($manager->upload($files)) {
             admin_toastr('导入成功');
         }
         //文件存储路径
         $filePath = "/data/www/cbb_new/storage/app/public/".$files[0]->getClientOriginalName();

         Excel::load($filePath, function($reader) {
             $data = $reader->all();
             //dd($data);
             //批量存储
             $value=[];
             $lastid = CardArticle::orderBy('id','desc')->limit(1)->value('id');
             foreach ($data as $k => $v ){
                 $lastid++;
                 //存储表格每行的值
                 $value['title']    =$v['title'];
                 $value['dt'] =date('Y-m-d H:i:s');
                 $value['seotitle'] =$v['seotitle'];
                 $value['keywords'] =$v['keywords'];
                 $value['author']   =$v['author'];
                 $value['class_id'] = intval($v['class_id']);
                 $value['bank_id']  =intval($v['bank_id']);
                 $value['content']  =$v['content'];
                 $value['views']    =intval($v['views']);
                 $value['order']    =intval($v['order']);
                 $value['generate_url']=  '/cardnews/cardnews_'.$lastid.'.shtml';
                 $value['status']   =intval($v['status']);

                 CardArticle::create($value);
             }
         });

     } catch (\Exception $e) {
         admin_toastr($e->getMessage(), 'error');
     }
     return back();
 }

    6. 写路由:admin\routes.php  我的方法写在CardArticleController控制器里面

$router->any('cardnewsimport', 'CardArticleController@import');

    7. Models里面添加要导入的字段

protected $fillable = ['xx','xx',...]

    完成 看看效果 表格文件/导入后截图

image.png

image.png


  • 上一条:
    国内最新最便捷的使用Google方法,无需翻墙,直接输入google.com
    下一条:
    关于猴子选大王的面试题
  • 昵称:

    邮箱:

    2条评论 (评论内容有缓存机制,请悉知!)
    最新最热

    这应该算是比较详细了吧, 你是哪一步没看懂吗?

    博主  2020-12-29 17:51:06 赞 (0)

    咋导入excel

    唐伯虎  2020-12-28 17:53:22 赞 (1)
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 9.24版本发布(0个评论)
    • Laravel collect集合中获取二维数组中键值功能示例代码(0个评论)
    • lumen中验证类的实现及使用流程步骤(0个评论)
    • 构建你自己的Laravel扩展包的流程步骤(0个评论)
    • Laravel 9.23版本发布(0个评论)
    • 近期文章
    • Laravel 9.24版本发布(0个评论)
    • windows系统phpstudy环境中安装amqp拓展流程步骤(0个评论)
    • windows10+docker desktop使用docker compose编排多容器构建dnmp环境(0个评论)
    • windows10+docker desktop运行laravel项目报错:could not find driver...(0个评论)
    • windows10+docker desktop报错:docker: Error response from daemon: user declined directory sharing(0个评论)
    • go语言中Pat多路复用器路由功能示例代码(0个评论)
    • go语言中HttpRouter多路复用器路由功能示例代码(0个评论)
    • js中使用Push.js通知库将通知推送到浏览器(0个评论)
    • Laravel collect集合中获取二维数组中键值功能示例代码(0个评论)
    • 10分钟设置免费远程桌面(0个评论)
    • 近期评论
    • nkt 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 用了三分钟就被禁了,直接阿里云服务器22端口都禁了..
    • 熊丽 在

      安装docker + locust + boomer压测环境实现对接口的压测中评论 试试水..
    • 博主 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 @test  也可能是国内大环境所至,也是好事,督促你该研究学习新技术..
    • test 在

      阿里云香港服务器搭建自用vpn:Shadowsocks使用流程步骤中评论 打了一次网页,然后再也打不开了。。是阿里云的缘故吗?..
    • 博主 在

      centos7中Meili Search搜索引擎安装流程步骤中评论 @鹿   执行以下命令看看你的2.27版本是否存在strin..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客