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

lucatamtam picture lucatamtam  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

bahmanyaghoobi picture bahmanyaghoobi  路  3Comments

octoxan picture octoxan  路  3Comments

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments