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!
N/A
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
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.
```
class CartageImport {
public function __construct(agentBilling $cartage)
{
$this->cartage = $cartage;
}
....
}