当前位置: 首页 > 知识库问答 >
问题:

将Excel多个表单输入导入到Laravel中的同一mysql表中

谷良弼
2023-03-14

我在Laravel 7中使用maatsite的Excel导入库将文件分析和支持数据导入到“分析”数据库表中,它工作得很好。但是,在我选择要导入的Excel文件的视图页面上,我有一个选择下拉和输入文本。我在各自的模型中创建了has很多和属性关系。

处理控制器:

//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

非常感谢你

共有1个答案

曹智
2023-03-14

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

return Profiling::create([
            'terminalid' => $row['terminalid'],
            'merchant_name_1' => $row['merchant_name_1'],
            'merchant_name_2' => $row['merchant_name_2'],
            'pemohon' => request('pemohon'),
            'brand_id' => request('brand_id'),
            'user_id' => $row['user_id'],
            ]);
 类似资料:
  • 通过下面的函数,您可以通过PHP将数据从Excel电子表格导入数据库。 表包含以下字段: 但我需要以下列:以导入另一个发票表,该表将包含以下字段: 其中client_id将接收客户端id。 我使用以下方法导入: 控制器 模型

  • 问题内容: 更新 在我发布此问题之后的第二秒,由于对结果查询的语法突出显示,我看到了出了什么问题:该字符串未以闭合斜线开头。现在我将其更改为: 但是,这提出了一个新问题:为什么PDO对象没有为此向我吐出错误?手动执行查询肯定会返回一个错误,指出没有名为的字段,最后是逗号。为什么我没有收到任何错误?有任何想法吗? PS:关于解决我的问题的SO语法突出显示方面有什么想法吗?:-) 我将原始问题留作参考

  • 我有两个表,一个是user,它是父表,另一个是posts(child)表,它的coulmns是 我已经说明了

  • 问题内容: 将csv文件上传到mysql表的最佳/最快方法是什么?我想将第一行数据用作列名。 发现了这一点: 如何将CSV文件导入MySQL表 但是唯一的答案是使用GUI而不是Shell? 问题答案: 您无需编写脚本即可从CSV文件中提取信息,而是可以直接将MYSQL链接到该文件并使用以下SQL语法上传信息。 要将Excel文件导入MySQL,请先将其导出为CSV文件。从生成的CSV文件中删除CS

  • 问题内容: 我正在使用SQL Server 2005。 我正在将数据从当前数据库(单个表)迁移到新数据库(规范化-许多表)。在新数据库中,我有一个基表(我们称它为“ BaseTable”)和多个其他表(我们称它们为和)。来自旧数据库的某些数据将转到BaseTable,而另一些将转到其他两个。BaseTable与DependentA和DependentB都具有一对一关系,使用它们的ID作为外键。 所