我想問一下。
我想用laravel-excel包來實作excel匯入功能。其中,我匯入的資料與另一個表有關系值。
所以,當我匯入excel時,它將把資料保存在兩個表中。
第一個表,存盤資料,如名稱和檔案路徑。而第二個表,保存了匯入的excel檔案的詳細資訊,并將其關系添加到第一個表。
下面是我的兩個表
booked_vouchers:- id
- id
- name <
- 路徑
預訂的優惠券細節:
- id
- booked_voucher_id
- 優惠券代碼
- 課程代碼
- 課程代碼 。
- 用戶名稱 。
下面是我在控制器、視圖和匯入檔案中的代碼。
表單
<form action="{ route('booked.import')}}" method="POST" enctype="multipart/form-data">
尊敬的女士們、先生們
<div class="form-group" >
<label for="name"> Name</label>
<input name="name" type="text" class="form-control" required>
</div>
<div class="form-group" >
<label for="file"> File</label>。
<input name="file" type="file" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary" > 匯入</按鈕>。
</form>
Controller
public function import(Request $request)
{
$validator = Validator::make($request->all(), [
'file' => 'required|mimes:csv,xlx,xls,xlsx'if ($validator-> fails()) {
return back()->with('toast_error', $validator-> messages()-> all()[0])-> withInput();
}
$data = new BookedVoucher()。
$data->name = $request->name。
$fileName = time().'_'.$request-> file->getClientOriginalName()。
$filePath = $request->file('file')-> storeAs('reports', $fileName, 'public') 。
$data->file = $filePath;
$data->created_by = Auth::user()->id。
if ($data-> save()) {
Excel::import(new BookedVoucherDetailImport, $request->file('file') )。)
}
return redirect()->back()->with('success','Data have been imported') 。
}
BookedVoucherDetailImport.php
<?php
namespace AppImports。
use AppModelBookedVoucher;
使用 AppModelBookedVoucherDetail。
use MaatwebsiteExcelConcernsToModel;
使用 MaatwebsiteExcelConcernsWithStartRow。
class BookedVoucherDetailImport implements ToModel, WithStartRow[/span].
{
/**。
* @return int
*/
public function startRow()。int
{
return 2;
}
/**.
* @param array $row
*
* @return IlluminateDatabaseEloquentModel|null
*/
public function model(array $row)
{
$data = BookedVoucher::first();
return new BookedVoucherDetail([
'booked_voucher_id' => $data-> id,
'course_code' => $row[0] 。
'voucher_code' => $row[1] 。
'user_name' => $row[2] 。
'status' => 'OK'
]);
}
}
如何將booked_voucher_id值設定為與excel匯入程序前已保存的booked_voucher表中的id值相同?
*目前,如果我使用像BookedVoucherDetailImport.php檔案中的代碼,booked_voucher_details表中的booked_voucher_id值結果總是不正確。
uj5u.com熱心網友回復:
你可以將BookedVoucher傳遞給Import類:
Excel:: import(new BookedVoucherDetailImport($data), $request->file('file') ) 。
然后更新你的BookedVoucherDetailImport為:
<?php
namespace AppImports。
use AppModelBookedVoucher;
use AppModelBookedVoucherDetail;
use MaatwebsiteExcelConcernsToModel;
使用 MaatwebsiteExcelConcernsWithStartRow。
class BookedVoucherDetailImport implements ToModel, WithStartRow[/span].
{
/**。
* @var BookedVoucher
*/
protected $bookedVoucher;
/**.
* @param BookedVoucher $bookedVoucher
*/
public function __construct(BookedVoucher $bookedVoucher)
{
$this->bookedVoucher = $bookedVoucher;
}
/**.
* @return int
*/
public function startRow()。int
{
return 2;
}
/**.
* @param array $row
*
* @return IlluminateDatabaseEloquentModel|null
*/
public function model(array $row)
{
return new BookedVoucherDetail([
'booked_voucher_id' => $this-> bookedVoucher->id。
'course_code' => $row[0] 。
'voucher_code' => $row[1] 。
'user_name' => $row[2] 。
'status' => 'OK',
]);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/324235.html
標籤:
