可以向 excel 导入添加多个参数吗?
Possible to add multiple parameters to excel import?
我有一个包含 excel 文件上传和 select 选项的基本表单。
select 包含一些选项。我想将带有文件的用户的 selected 选项传递给我的 Excel::import
我尝试将另一个参数传递给我的导入,但它 returns 出错了。 (必须是字符串,给定的数组)
是否可以使用 Laravel excel 导入?
控制器
public function create(Request $request)
{
if($request->hasFile('file'))
{
$package = $request->input('package');
// how can I add $package to my import to insert it to my user model?
Excel::import(new AccountsImport, $request->file('file'));
return ['message' => 'Excel has been succesfully uploaded'];
}
}
帐户导入
class AccountsImport implements ToCollection, withHeadingRow
{
public function collection(Collection $rows)
{
$rows->each(function($row, $key) {
Account::create([
'name' => $row['student'],
'email' => $row['e_mail'],
// Want it to be possible to add my package here
//'package' => $package
]);
});
}
}
也许您可以将自定义数据传递给您的 AccountsImport
class?
您将拥有这样的数据数组:
$data = [
'package' => $package,
// other data here
];
然后你会做这样的事情:
Excel::import(new AccountsImport($data), $request->file('file'));
这将需要对您的导入进行一些更改 class。
class AccountsImport implements ToCollection, withHeadingRow
{
private $data;
public function __construct(array $data = [])
{
$this->data = $data;
}
public function collection(Collection $rows)
{
$rows->each(function($row, $key) {
Account::create(array_merge([
'name' => $row['student'],
'email' => $row['e_mail'],
// Want it to be possible to add my package here
//'package' => $package
], $this->data));
});
}
}
我查看了 Laravel Excel 的 API 并没有看到适合这个的东西,但这应该有用。
我有一个包含 excel 文件上传和 select 选项的基本表单。
select 包含一些选项。我想将带有文件的用户的 selected 选项传递给我的 Excel::import
我尝试将另一个参数传递给我的导入,但它 returns 出错了。 (必须是字符串,给定的数组)
是否可以使用 Laravel excel 导入?
控制器
public function create(Request $request)
{
if($request->hasFile('file'))
{
$package = $request->input('package');
// how can I add $package to my import to insert it to my user model?
Excel::import(new AccountsImport, $request->file('file'));
return ['message' => 'Excel has been succesfully uploaded'];
}
}
帐户导入
class AccountsImport implements ToCollection, withHeadingRow
{
public function collection(Collection $rows)
{
$rows->each(function($row, $key) {
Account::create([
'name' => $row['student'],
'email' => $row['e_mail'],
// Want it to be possible to add my package here
//'package' => $package
]);
});
}
}
也许您可以将自定义数据传递给您的 AccountsImport
class?
您将拥有这样的数据数组:
$data = [
'package' => $package,
// other data here
];
然后你会做这样的事情:
Excel::import(new AccountsImport($data), $request->file('file'));
这将需要对您的导入进行一些更改 class。
class AccountsImport implements ToCollection, withHeadingRow
{
private $data;
public function __construct(array $data = [])
{
$this->data = $data;
}
public function collection(Collection $rows)
{
$rows->each(function($row, $key) {
Account::create(array_merge([
'name' => $row['student'],
'email' => $row['e_mail'],
// Want it to be possible to add my package here
//'package' => $package
], $this->data));
});
}
}
我查看了 Laravel Excel 的 API 并没有看到适合这个的东西,但这应该有用。