将Excel +许多表单输入导入Laravel中的同一mysql表

问题描述

我正在Laravel 7中使用maatwebsite的Excel导入库将文件分析和支持数据导入“分析”数据库表中,并且效果很好。但是,在我选择要导入的Excel文件的“查看”页面上,我有一个选择下拉列表并输入文本。我已经在各个模型中创建了hasMany和belongsTo关系。

ProcessingController:

//view import profiling page
public function profiling_import()
{
    // mengambil data edc
    $edc = Edc::all();

    // mengambil data brand
    $brand = Brand::all();
    
    return view('processing.profiling_import',['edc' => $edc,'brand' => $brand]);  
}



public function profiling_import_excel(Request $request) 
{
    $messages = [     
        'required' => 'Bidang ini harus di isi',];

    // validasi
    $this->validate($request,[
        'file' => 'required|mimes:xls,xlsx','pemohon' => 'required','brand_id' => 'required'
    ],$messages);
    
    
    if ($request->hasFile('file')) {
    
    $pemohon = $request->pemohon;
    $brand_id = $request->brand_id;
    $user_id = $request->user_id;

        Excel::Import(new \App\Imports\ProfilingImport,$request->file('file'),$brand_id);
        return redirect('/profiling')->with("sukses","Data profiling berhasil di import");

    }

    return redirect('/profiling')->with("gagal","Silakan pilih file import kembali");

}

配置文件导入:

public function collection(Collection $collection)
{
    $collection = $collection->toArray();
    foreach ($collection as $key => $row){
        if($key >= 2){
            
            $pemohon = request('pemohon');
            $brand_id = request('brand_id');
            $user_id = request('user_id');

            $row['pemohon'] = $pemohon;
            $row['brand_id'] = $brand_id;
            $row['user_id'] = $user_id;
                    
            //dd($collection);

            return Profiling::create([
                'terminalid' => $row['terminalid'],'merchant_name_1' => $row['merchant_name_1'],'merchant_name_2' => $row['merchant_name_2'],'pemohon' => $row['pemohon'],'brand_id' => $row['brand_id'],'user_id' => $row['user_id'],]);
        }
    }
 }

profiling_import.blade:

<div class="card-body">
                        <form method="post" action="{{ url('/profiling/import_excel') }}" enctype="multipart/form-data"> 
                            @csrf
                            <div class="form-group"> 
                                <label>Pemohon</label> 
                                <input type="text" name="pemohon" class="form-control" placeholder="Isi dengan nama pemohon ..." value="{{ old('pemohon') }}" autofocus> 
     
                                @if($errors->has('pemohon')) 
                                <span class="text-danger"> 
                                    <strong>{{ $errors->first('pemohon') }}</strong> 
                                </span> 
                                @endif      
                            </div>

                            <div class="form-group"> 
                                <label>Brand</label> 
                                <select id="brand_id" name="brand_id" class="form-control">
                                    <option value="0" disabled="true" selected="true">- Pilih Brand EDC</option>
                                    @foreach($brand as $b)
                                        <option value="{{ $b->id }}">{{ $b->brand}} ({{$b->principal }})</option>
                                    @endforeach
                                </select>

                                @if($errors->has('brand_id')) 
                                <span class="text-danger"> 
                                    <strong>{{ $errors->first('brand_id') }}</strong> 
                                </span> 
                                @endif      
                            </div>

                            <div class="form-group">
                                <label for="">File Profiling (.xls,.xlsx)</label>
                                <input type="file" class="form-control" name="file">
                                <p class="text-danger">{{ $errors->first('file') }}</p>
                                   
                                <div class="form-group">
                                    <input type="hidden" name="user_id" value="{{ Auth::user()->name }}" />
                                    <input type="reset" class="btn btn-warning" value="Reset" autofocus>
                                    <input type="submit" class="btn btn-primary" value="Save" autofocus>
                                </div>


                            </div>
                        </form>
                    </div>

在Excel文件中:

| terminalid | merchant_name_1 | merchant_name_2 |
| 12345678   | Matahari Store  | Jakarta         |
| 98765432   | Amazon Store    | Bandung         | 

查看:

Pemohon : Ardi            (input text)
Brand   : Ingenico      v (select)
File    : Choose File     (input file)

Save

问题是pemohon,品牌和用户数据未填充到数据库中。

array:4 [▼
  0 => array:54 [▶]
  1 => array:54 [▶]
  2 => array:54 [▼
       0 => "98765432"
       1 => "Amazon Store"
       2 => "Bandung"

       3 ? Ardi
       4 ? Ingenico
       5 ? Admin

非常感谢

解决方法

这是因为您可以在项目中的任何位置访问请求对象。这可能有效:

 div {
    border: 1px solid black;
    width: 190px;
    display: flex;
}
span {
    color: red;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...