laravel-admin文件上传 数据导出excel

laravel-admin文档写的非常详尽,,如果不是特殊需求根本不需要额外查询资料,这里只写一下自己习惯 方便自己快速使用,读者建议看文档走一遍。

文件上传

laravel-admin中文文档-文件上传链接

config/filesystems.php

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
        'admin' => [
	        'driver'     => 'local',
	        'root'       => public_path(),
	        'visibility' => 'public',
	        'url' => env('APP_URL').'/',
	    ],

    ],

控制器表单:

$form->multipleImage('hetong', __('合同'))->move('/upload/hetong/'.date('Y-m-d H:i:s'))->removable()->sortable();

数据导出excel

laravel-admin中文文档-数据导出链接

$grid->export(function ($export) {
            // var_dump($export);exit;
            $export->filename('Filename.csv');
        
            // $export->except(['column1', 'column2' ]);
        
            // $export->only(['column3', 'column4' ]);
        
            $export->originalValue([ 'statusx','zhouqi']);
        
            $export->column('cpname', function ($value, $original) {
                return $original;
            });
            
        });

如果想导出多表关联数据可用以下方式:

<?php

namespace App\Admin\Extensions\Export;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class UsersExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '用户列表.xlsx';

    protected $columns = [
        'id'                    => 'ID',
        'name'                  => '姓名',
        'status'                => '状态',
        'profile.homepage'      => '主页',
    ];

    public function map($user) : array
    {
        return [
            $user->id,
            $user->name,
            $user->status ? 'yes' : 'no',           // 字段数据替换
            $user->homepage,    // 读取关联关系数据
        ];
    }
}

然后在Table中使用这个导出类:

use App\Admin\Extensions\PostsExporter;

$grid->exporter(new PostsExporter());

多表联查(两表数据联查合成一条导出多条表)类似外连接查询

数据结构为总表和分表组成一条信息



<?php

namespace App\Admin\Extensions\Export;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class UsersExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '用户列表.xlsx';

    protected $columns = [
        'id'                    => 'ID',
        'name'                  => '姓名',
        'profile.nickname'      => '昵称',
        'status'                => '状态',
        'profile.homepage'      => '主页',
        'profile.code'          => '识别码',
    ];

   public function map($user) : array
    {
        $data=\DB::table('info')->where('userid',$user->id)->get(['code','nickname','created_at','updated_at']);
         $result=array();
         foreach($data as $da){
            

            $da =[
                $user->id,
                $user->name,
                $username->nickename=$da->nickename,
                $user->status ? 'yes' : 'no',           // 字段数据替换
                $user->homepage="s23赛季",    // 读取关联关系数据
                $user->code=$da->code
            ];
            $result[]=$da;    


         } 
         return $result;
    }
}

多表联查(两表分为总分结构导出多条数据)

数据结构为一条总表数据组成一条信息,多条分表数据组成多条信息

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter; 
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
class BooksExporter extends ExcelExporter implements WithMapping,WithColumnWidths
{
    protected $fileName = '书籍码.xlsx';

    protected $columns = [
        'code'=>'sku',
        'title'=>'全称',
        'titleyun'=>'仓库名称',
        'stock'=>'现有库存',
        'checkno'=>'未审核',
        'yungd' => '云仓挂单',
        'localgd' => '本地挂单',
        'stockque' => '实际库存',
        'avg3'=>'三天平均',
        'extradays'=>'可用天数',
        'profile.biaoshi'=>'标识',
        
    ];
    
    public function map($order) : array
    {   
        
        $data=\DB::table('bookbinds')->where('code',$order->code)->get(['codebind','stock','stockque','avg3','extradays','checkno','yungd','localgd']);
        
        $result=[
            [
                $order->code,
                $order->title,
                $order->titleyun,
                $order->stock==0?"0":$order->stock,
                $order->checkno==0?"0":$order->checkno,
                $order->yungd==0?"0":$order->yungd,
                $order->localgd==0?"0":$order->localgd,
                $order->stockque==0?"0":$order->stockque,
                $order->avg3==0?"0":$order->avg3,
                $order->extradays==0?"0":$order->extradays,
                $order->biaoshi="初始",
            ]    
        ];
        foreach ($data as $da){
            $da=[
                $order->code=$da->codebind,
                $order->title,
                $order->titleyun,
                $order->stock=$da->stock==0?"0":$da->stock,
                $order->checkno=$da->checkno==0?"0":$da->checkno,
                $order->yungd=$da->yungd==0?"0":$da->yungd,
                $order->localgd=$da->localgd==0?"0":$da->localgd,
                $order->stockque=$da->stockque==0?"0":$da->stockque,
                $order->avg3=$da->avg3==0?"0":$da->avg3,
                $order->extradays=$da->extradays==0?"0":$da->extradays,
                $order->biaoshi="绑定",
            ];
            $result[]=$da;
        }
        return $result;
           
    }
    
     
     
}
0 评论
内联反馈
查看所有评论