I need to return to controlling the id of the products entered by the excel import.
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);
}
I have not found a way to return variables in excel import. Thanks for your help.