Laravel-excel: Get parameter from controller in import class

Created on 27 Nov 2018  路  9Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

  • PHP version: 7.2
  • Laravel version: 5.7
  • Package version: 3.1

Description


I wnat to get my parameters from $request in class import. what should i do ?
this is my controller
capture

this is my class import
capturea

Additional Information

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

Most helpful comment

Hi
In Controller
$data = Excel::toArray(new FileImport($request->month), $file);
and in Import class

namespace App\Imports;

class OrderImport extends BaseImport
{
protected $_month = null;

public function __construct($month) {
   $_month = $month;
}

public function model(array $row) {
    $m = $this->_month;
}

}

All 9 comments

You can import data from controller by
(new FileImport($request->month))
and get it in Import file as function __contruct
protected $_month = null; public function __contruct($month) { $_month = $month; }

You can import data from controller by
(new FileImport($request->month))
and get it in Import file as function __contruct
protected $_month = null; public function __contruct($month) { $_month = $month; }

can you explain more, please? I face the same problem

Hi
In Controller
$data = Excel::toArray(new FileImport($request->month), $file);
and in Import class

namespace App\Imports;

class OrderImport extends BaseImport
{
protected $_month = null;

public function __construct($month) {
   $_month = $month;
}

public function model(array $row) {
    $m = $this->_month;
}

}

Thanks @RibikiH! I hope @abdrozak20 has enough information now. Closing this ticket.

Thanks, Why isn't all this in the docs?

Thanks @patrickbrouwers!

Hi
In Controller
$data = Excel::toArray(new FileImport($request->month), $file);
and in Import class

namespace App\Imports;

class OrderImport extends BaseImport
{
protected $_month = null;

public function __construct($month) {
   $_month = $month;
}

public function model(array $row) {
    $m = $this->_month;
}

}

there is a error, to fix use:

public function __construct($month) {
$this->_month = $month;
}

nice!

Was this page helpful?
0 / 5 - 0 ratings