Laravel-excel: [QUESTION] Get current row on rules function using WithValidation trait

Created on 9 Jul 2020  路  5Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.3
  • Laravel version: 7.12.0
  • Package version: 3.1.19

Description

My question is simple, is it possible to get the value of the row on validation ?

Thank you by advance.

question

All 5 comments

Currently not. You could always do the validation yourself inside the collection/onEachRow/Model method if you need that kind of control.

Okay thank you, I'm going to do this in the model method, is there a way to find the row index ?

For now I added it with a global variable in my import class

private $startRow;

    public function __construct()
    {
        $this->startRow = 2;
    }
    public function model(array $row)
    {
        $this->startRow++;
    }

@HamzaDevz that's for current release the best option yes, however our next release will have a trait that does this for you automatically.

Sorry again @patrickbrouwers is it the proper way to add manually a failure ?
Here is what I did :

public function model(array $row)
{
        $this->startRow++;
        $purchaseRequestProduct = $this->purchaseRequest->purchaseRequestProducts()->whereSku($row['ean'])->first();

        if (!isset($purchaseRequestProduct))
        {
            $this->failures[] = new Failure($this->startRow, 'ean', [ 'This product does not exist.' ], $row);
            return;
        }
}

Thank you.

I think so yes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhghazaliakbar picture muhghazaliakbar  路  3Comments

lucatamtam picture lucatamtam  路  3Comments

ellej16 picture ellej16  路  3Comments

daraghoshea picture daraghoshea  路  3Comments

thearabbit picture thearabbit  路  3Comments