码迷,mamicode.com
首页 > Web开发 > 详细

PHP知识点草稿

时间:2018-04-16 12:10:11      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:util   try   file   created   demo   service   reac   esc   成功   

1、laravel Excel批量导入数据到DB

<?php
namespace App\Http\Controllers\Module;

use App\Http\Controllers\Controller;
use App\Models\ModuleInterface;
use App\Services\Util\Msg;

class ImportController extends Controller
{
    /**
     * Excel批量导入数据到DB
     * @return \Illuminate\Http\JsonResponse
     */
    public function index() 
    {
        try {
            $file = ‘demo.xls‘;
            Excel::load($file, function($reader) {
                $sourceData = collect($reader->get())->toArray();
                $insertData = [];
                $dateTime = date(‘Y-m-d H:i:s‘, time());
                foreach ($sourceData as $key => $item) {
                    $insertData[] = [
                        ‘Fname‘         => $item[‘fname‘],
                        ‘Fdescribe‘     => $item[‘fdescribe‘],
                        ‘Fservice_id‘   => (int)$item[‘fservice_id‘],
                        ‘Fbusi_person‘  => ‘tayloryu‘,
                        ‘Fdevops‘       => ‘tayloryu‘,
                        ‘Fcreated_at‘   => $dateTime,
                        ‘Fcreator‘      => ‘tayloryu‘,
                    ];
                }
                ModuleInterface::insert($insertData);
            });
            
            return Msg::out(‘导入成功!‘);
        } catch (Exception $e) {
            return Msg::out($e->getMessage(), $e->getCode());
        }
    }
}
      

  

PHP知识点草稿

标签:util   try   file   created   demo   service   reac   esc   成功   

原文地址:https://www.cnblogs.com/huqi21st/p/8854954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!