Laravel-excel: [QUESTION] Passing Variable from Controller to Importer

Created on 8 Jan 2019  路  6Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

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

Description

If I have the following in my controller:

    public function excelImport(Request $request){
        $request->validate([
            'import_file' => 'required'
        ]);

        $cartage= agentBilling::findOrFail($request->cartage_id);
        Excel::import(new CartageImport, request()->file('import_file'));
        return back()->with('success', 'Updated Records successfully.');
    }

How would I go about passing the cartage variable to the import and then use it from within the Importer?

Thanks!

Additional Information

N/A

Most helpful comment

Just pass the cartage (or cartage_id) through the constructor of the import object. It's just a PHP object, you can do anything with it that you would do with normal PHP objects.

$cartage= agentBilling::findOrFail($request->cartage_id);
 Excel::import(new CartageImport($cartage), request()->file('import_file'));

```
class CartageImport {
public function __construct(agentBilling $cartage)
{
$this->cartage = $cartage;
}

....
}

All 6 comments

You would do it the following:

Excel::import(new CartageImport(request()->file('import_file')));

Then in your CartageImport

__construct($file) {
$this->file = $file;
}

then however you are doing the import reference via $this->file

Hi SeoRoman - I thank you for your help, but I don't see where you reference the $cartage variable which includes my model data. Thank you though for the help!

Just pass the cartage (or cartage_id) through the constructor of the import object. It's just a PHP object, you can do anything with it that you would do with normal PHP objects.

$cartage= agentBilling::findOrFail($request->cartage_id);
 Excel::import(new CartageImport($cartage), request()->file('import_file'));

```
class CartageImport {
public function __construct(agentBilling $cartage)
{
$this->cartage = $cartage;
}

....
}

I can't pass parameter also.
In controller.

$dataImport = DataImport::create($data);
$importer = new ScripImport($dataImport);
Excel::import(new ScripImport($dataImport), storage_path('app/'.$data['file_path']));](url)

In ScripImport.php

class ScripImport implements ToModel, WithHeadingRow, FromCollection, WithHeadings, ShouldAutoSize, WithMultipleSheets
{
    private $dataImport=null;
    public function __construct(DataImport $dataImport)
    {
        $this->dataImport=$dataImport;
    }

    public function sheets(): array
    {
        return [
            0 => new ScripImport(),
        ];
    }
    public function model(array $row)
    {
    }
   ...
}

It gives
Too few arguments to function App\Imports\ScripImport::__construct(), 0 passed in D:\xampp\htdocs\Fincook\app\Imports\ScripImport.php on line 30 and exactly 1 expected",

You are not passing it to the ScripImport.

class ScripImport implements ToModel, WithHeadingRow, FromCollection, WithHeadings, ShouldAutoSize, WithMultipleSheets
{
    private $dataImport=null;
    public function __construct(DataImport $dataImport)
    {
        $this->dataImport=$dataImport;
    }

    public function sheets(): array
    {
        return [
            0 => new ScripImport($this->dataImport),
        ];
    }
    public function model(array $row)
    {
    }
   ...
}

In controller

Excel::download(new UsersExport(2019), 'users.xlsx');

In laravel excel importer

class UsersExport implements FromCollection {
    private $year;

    public function __construct(int $year) 
    {
        $this->year = $year;
    }

    public function collection()
    {
        return Users::whereYear('created_at', $this->year)->get();
    }
}

for same, you can refer following official documentation link

https://docs.laravel-excel.com/3.1/architecture/objects.html#plain-old-php-object

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

bahmanyaghoobi picture bahmanyaghoobi  路  3Comments

dr3ads picture dr3ads  路  3Comments