Laravel Excel 使用 Maatwebsite Excel 包从视图导入附加列

Laravel Excel import using Maatwebsite Excel package with additional columns from View

我正在尝试将数据从 Excel 文件导入 MySQL。我的 table 有 2 个外键 project_idsite_id 导入时我从视图的下拉列表中选择这两个字段。有什么方法可以将这两个字段映射到我的导入 collection?请注意,导入文件中不存在这 2 个字段(出于完整性原因),但它们确实存在于 table.

Collection

namespace App\Imports;

use App\Proposal;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class ProposalsImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        return new Proposal([
            'building'  => $row['building'],
            'floor' => $row['floor'],
            'area'    => $row['area'],
            'room'    => $row['room'],
            'luminaire'    => $row['luminaire'],
            'actual_qty'    => $row['actual_qty'],
            'installed_qty'    => $row['installed_qty'],
        ]);
    }
}

控制器

public function import() 
{
    Excel::import(new ProposalsImport, 'proposals.xlsx');
}

查看

@extends('projectmanagement/proposals.base')
@section('action-content')
    <!-- Main content -->
    <section class="content">
        <div class="container">
            <div class="box">
                <div class="box-header">

                </div>
                <!-- /.box-header -->
                <div class="box-body" data-widget="box-refresh">
                    @if (session('status'))
                        <div style="padding-top: 0px;padding-bottom: 0px;"
                            class="alert alert_cust alert-success alert-dismissable fade in">{{ session('status') }}<a
                                href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>
                    @endif
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="{{ route('proposals.store') }}">
                            {{ csrf_field() }}
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group{{ $errors->has('project_id') ? ' has-error' : '' }}">
                                        <label for="project_id" class="col-md-4 control-label">Project Name</label>
                                        <div class="col-md-6">
                                            <select id="project_id" class="form-control select2" style="width: 100%;"
                                                name="project_id">
                                                <option value="0" disabled selected>Select Project</option>
                                                @foreach ($projects as $project)
                                                    <option value="{{$project->id}}">{{ $project->project_name }}</option>
                                                @endforeach
                                            </select>
                                            @if ($errors->has('project_id'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('project_id') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <br/>
                                    <div class="form-group{{ $errors->has('site_id') ? ' has-error' : '' }}">
                                        <label for="site_id" class="col-md-4 control-label">Site Name</label>
                                        <div class="col-md-6">
                                            <select id="site_id" class="form-control select2" style="width: 100%;"
                                                name="site_id">
                                                <option value="0" disabled selected>Select Site</option>
                                                @foreach ($sites as $site)
                                                    <option value="{{$site->id}}">{{ $site->site_name }}</option>
                                                @endforeach
                                            </select>
                                            @if ($errors->has('site_id'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('site_id') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <br/>
                                    <div class="form-group{{ $errors->has('proposal_file') ? ' has-error' : '' }}">
                                        <label for="proposal_file" class="col-md-4 control-label">Proposal File</label>
                                        <div class="col-md-6">
                                            <input id="proposal_file" type="file" class="form-control" name="proposal_file" required autofocus>

                                            @if ($errors->has('proposal_file'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('proposal_file') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <br/>
                                    <div class="form-group">
                                        <div class="col-md-4">
                                            
                                        </div>
                                        <div class="col-md-6">
                                            <button type="submit" class="btn col-sm-3 col-xs-5 btn-primary">Upload Proposal</button>
                                        </div>
                                    </div>
                                    <br/>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        @push('custom_scripts')
            <script>

            </script>
        @endpush
    @endsection

覆盖导入控制器的构造方法,发送你需要的所有参数,像这样:

class ProposalsImport implements ToModel, WithHeadingRow
{
    protected $project_id;
    protected $site_id;

    public function __construct($project_id, $site_id)
    {
        $this->project_id = $project_id;
        $this->site_id = $site_id;
    }

    public function model(array $row)
    {
        return new Proposal([
            'building'   => $row['building'],
            'project_id' => $this->project_id,
            'site_id'    => $this->site_id
        ]);
    }
}

然后,像这样从您的控制器中调用它:

public function import(Request $request) 
{
    Excel::import(new ProposalsImport($request->project_id, $request->site_id), 'proposals.xlsx');
}