我在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
非常感谢你
这是因为您可以在项目中的任何位置访问请求对象。这可能会起作用:
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。 我使用以下方法导入: 控制器 模型
我有两个表,一个是user,它是父表,另一个是posts(child)表,它的coulmns是 我已经说明了
问题内容: 更新 在我发布此问题之后的第二秒,由于对结果查询的语法突出显示,我看到了出了什么问题:该字符串未以闭合斜线开头。现在我将其更改为: 但是,这提出了一个新问题:为什么PDO对象没有为此向我吐出错误?手动执行查询肯定会返回一个错误,指出没有名为的字段,最后是逗号。为什么我没有收到任何错误?有任何想法吗? PS:关于解决我的问题的SO语法突出显示方面有什么想法吗?:-) 我将原始问题留作参考
问题内容: 将csv文件上传到mysql表的最佳/最快方法是什么?我想将第一行数据用作列名。 发现了这一点: 如何将CSV文件导入MySQL表 但是唯一的答案是使用GUI而不是Shell? 问题答案: 您无需编写脚本即可从CSV文件中提取信息,而是可以直接将MYSQL链接到该文件并使用以下SQL语法上传信息。 要将Excel文件导入MySQL,请先将其导出为CSV文件。从生成的CSV文件中删除CS
嗨,我需要一些帮助来编写VBA代码以自动将多个Excel(csv格式)文件导入Access。我需要导入的每个文件中的一个工作表上有2个数据范围。两个数据范围都有动态行数。Excel文件,让我们称它们为“SourceDataXXX. csv”,所有数据都在同一个工作表上,让我们称它为“InputData”。第一组数据总是从单元格A4开始,是7列数据(在单元格G4结束)。这组数据有可变数量的数据行。在