laravel - Laravel导入Excel,传递变量时出错
问题描述
我想导入一些类别,但我还需要父类别 ID。
public function importCategory(Request $request, $cat_id){
$import = new CategoryImport($cat_id);
$import->import($request->file);
if ($import->failures()->count() > 0) {
$message = '';
foreach ($import->failures() as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
$failure->values(); // The values of the row that has failed.
}
return redirect()->back();
} else {
return redirect()->back()->with('success', sprintf('Success'));
}
}
这是 CategoryImport.php
class CategoryImport implements WithHeadingRow, WithValidation, SkipsOnFailure,OnEachRow
{
use Importable, SkipsFailures;
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected $cat_id = null;
public function __construct( $cat_id) {
$category_id = $cat_id;
}
public function onRow(Row $row)
{
$row=$row->toArray();
Category::create([
'name' => $row['name'],
'image' => $row['image'],
'business_category_id' => $this->category_id,
]);
}
}
所以这是错误,它说未定义的属性,我试图弄清楚但不明白。
未定义的属性:App\Imports\CategoryImport::$category_id
解决方案
您没有正确设置 cat_id 。在您的 CategoryImport 中,您应该有:
protected $category_id = null;
public function __construct($cat_id) {
$$this->category_id = $cat_id;
}
推荐阅读
- android - 上传Apk到AppGallery 华为失败
- r - 将 if else 语句与两个条件结合起来
- android - 材料日期选择器选择的日期圈不在中心,如何使其适合中心?
- c# - 隐式转换不适用于列表c#
- quarkus - 无法在 jaeger ui 上获取详细跟踪消息(使用 quarkus)
- linux - 错误:在 fabric 1.2 中找不到模块“cors”
- r - Why does R sometimes let me use get(string) to refer to a data.table and sometimes not?
- java - 使用 Spring 和 Apache FreeMarker 访问 CSS 的路径问题
- arrays - How to solve this Reactjs Eslint warning about 'index' is defined but never used
- css - CSS Border iOS 外观问题的解决方案?