Laravel-excel: passing parameter to ToMOdel

Created on 5 Dec 2018  路  2Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions 3.1.0

  • PHP version: 7.1
  • Laravel version: 5.7
  • Package version: 3.1.0

Description

hi,
it's possible to passing a param to a ToModel?

br
Max

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

Most helpful comment

Just pass the param to the constructor of your import class and it should work.
E.g.

class UsersImport implements ToModel, WithHeadingRow
{
    private $columnName;

    public function __construct($param)
    {
        $this->columnName = $param;
    }

    public function model(array $row)
    {
        return new User([
            'name'     => $row[$this->columnName]
        ]);
    }
}

All 2 comments

Just pass the param to the constructor of your import class and it should work.
E.g.

class UsersImport implements ToModel, WithHeadingRow
{
    private $columnName;

    public function __construct($param)
    {
        $this->columnName = $param;
    }

    public function model(array $row)
    {
        return new User([
            'name'     => $row[$this->columnName]
        ]);
    }
}

very nice :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pamekar picture pamekar  路  3Comments

ellej16 picture ellej16  路  3Comments

gamevnlc picture gamevnlc  路  3Comments

contifico picture contifico  路  3Comments

octoxan picture octoxan  路  3Comments