I am using excel 3.1 in Laravel for importing excel files. I have included a validation for the name field. So whenever the name field is duplicating it will not import that row data to DB. everything works fine up to here. but the problem is I want to show success message in the controller. But the message is not showing and not showing any other error messages. This is my import function
namespace App\Imports;
use App\Products;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\Importable;
use App\Categories;
use App\Brand;
use App\Unit;
use Maatwebsite\Excel\Concerns\WithValidation;
// use Illuminate\Validation\Rule;
// use Throwable;
class ExcelImport implements ToModel, WithStartRow, WithValidation, SkipsOnError
{
use Importable, SkipsErrors;
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
$name = $row[0];
$des = $row[1];
$product = new Products([
'name' => $row[0],
'description' => $row[1],
]);
return $product;
}
public function startRow(): int
{
return 2;
}
public function rules(): array{
return[
'0' => 'unique:products,name',
];
}
}
this is my controller
public function uploadfile(Request $request)
{
$this->validate($request, [
'file' => 'required',
]);
if($request->hasfile('file'))
{
foreach($request->file('file') as $file)
{
$import = new ExcelImport;
$import = $import->import($file);
}
//if the import is successful then I want to show some messages here
}
else{
return back()->with('error','File contains invalid data. Please upload a valid file.');
}
}