0

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.');
        }
    }
rkv
  • 1
  • 3
  • Welcome to Stack Overflow! Please read [what this site is about](https://stackoverflow.com/about) and "[How to ask](https://stackoverflow.com/questions/how-to-ask)" before asking a question. More specifically the section _Help others reproduce the problem_ – Clément Baconnier Apr 06 '21 at 08:40
  • _I am not getting anything back to the function in controller_ Can you be more specific? – Clément Baconnier Apr 06 '21 at 08:42
  • @ClémentBaconnier okay.I have updated the question. – rkv Apr 06 '21 at 10:15
  • 1
    Does it reach `return redirect()->back()->with('success', '....');` ? – Clément Baconnier Apr 06 '21 at 10:22
  • @ClémentBaconnier No – rkv Apr 06 '21 at 10:37
  • Does anyone know the answer?Help me – rkv Apr 07 '21 at 04:26
  • Sorry, I'm not able to help you with the current question as I don't even know where you have an issue with your code. Your description may be too broad to understand in my opinion. By the way, I'm not sure you want to `return redirect()->back()` the user in a `foreach` loop. – Clément Baconnier Apr 07 '21 at 07:46

0 Answers0