【发布时间】:2021-06-21 04:51:35
【问题描述】:
我需要返回控制excel导入输入的产品的id。
ProductImport.php
<?php
namespace App\Imports;
use App\Product;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class ProductImport implements ToCollection {
public function collection(Collection $rows) {
foreach ($rows as $row) {
$product = Product::create([
'name' => $row[0],
'detail' => $row[1]
])->id;
}
return $product;
}
}
ProductController.php
public function uploadProducts(Request $request) {
$request->validate([
'import_file' => 'required|file|mimes:xls,xlsx'
]);
$path = $request->file('import_file');
$import = new ProductImport;
Excel::import($import, $path);
//Here, how can I return the id of the products that were entered?
return response()->json(['message' => 'uploaded successfully'], 200);
}
我还没有找到在 excel 导入中返回变量的方法。感谢您的帮助。
【问题讨论】:
标签: php laravel laravel-excel