webdevask.com

Menu

  • Home
  • New
  • Add question

php - Laravel - Excel: Add validation and display the message

View Original

one text

We implemented the maatwebsite because in our module there's import capability.

Now I want to add a validation for each column to check if it has a value.

Here's what my import file looks like.

enter image description here

Controller

public function import()
{
        $this->validate([
            'file' => 'required|mimes:csv,txt|max:3072|file'
        ]);

        Excel::import(new Import, $this->file);
}

Import

public function collection(Collection $rows)
{
    $count                                                      = count($rows);
    // if the uploaded file has 2 or more value
    if($count > 1)
    {
        $array                                                  = [];
        foreach ($rows as $r => $row) {
            //skip the title header
            if($r != 0)
            {
                if($row[0] == '')
                {
                  //display error message
                }
            }
        }
        

    }
    else
    {
        // $this->dispatchBrowserEvent('swal:redirect',[
        //     'position'                                              => 'center',
        //     'icon'                                                  => 'success',
        //     'title'                                                 => 'Successfully changed filter!',
        //     'showConfirmButton'                                     => 'true',
        //     'timer'                                                 => '1500',
        //     'link'                                                  => '#'
        //  ]);

        return false;
    }
    
}

Notes: Reason why I didn't use the mapping because there's a condition when the imported file has/have same number it will merge/combine

Question: How can I display the error message?

Source

See also:

  • php สร้าง Google ชีตจากบัญชีบริการ

  • php - Laravel 8.0 Installation Failed

  • javascript - How to include HTML data (tags) as value in "arrayToDataTable" of Google Pie Chart?

  • how to Grouping array in php

  • php - vender folder from my laravel project has more 1k error

© 2021
E-mail: contact@webdevask.com
Back to top