Laravel - local.ERROR: Error: Call to undefined function App\Imports\create() in Excel Import
Laravel - local.ERROR: Error: Call to undefined function App\Imports\create() in Excel Import
我正在使用 Laravel-8 和 Maatwebsite-3.1 包进行 excel 上传:
我有这个Excelsheet(数据更感谢):
进口:
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Validators\Failure;
use Throwable;
class EmployeeImport extends DefaultValueBinder implements OnEachRow, WithStartRow, SkipsOnError, WithValidation, SkipsOnFailure, WithEvents, WithCustomValueBinder
{
// set the preferred date format
private $date_format = 'Y-m-d';
// set the columns to be formatted as
private $date_columns = ['E'];
use Importable, SkipsErrors, SkipsFailures;
public function onRow(Row $row)
{
$rowIndex = $row->getIndex();
if($rowIndex >= 1000)
return; // Not more than 1000 rows at a time
$row = $row->toArray();
$employee_data = [
'first_name' => $row[0],
'other_name' => $row[1] ?? '',
'last_name' => $row[2],
'email' => preg_replace('/\s+/', '', strtolower($row[3])),
'dob' => $this->transformDate($row[4]),
];
$employee = create(Employee::class, $employee_data);
if (User::where('email', '=', $employee->email)->exists()) {
$user = User::update([
'first_name' => $employee->first_name,
'last_name' => $employee->last_name,
'active' => 1,
]);
}else{
$user = User::create([
'email' => $employee->email,
'first_name' => $employee->first_name,
'last_name' => $employee->last_name,
'active' => 1,
]);
}
$employee->update([
'user_id' => $user->id,
]);
}
public function startRow(): int
{
return 2;
}
public function customValidationAttributes()
{
return [
'0' => 'First Name',
'1' => 'Other Name',
'2' => 'Last Name',
'3' => 'Email',
'4' => 'Date of Birth',
];
}
public function rules(): array
{
return [
'*.0' => [
'required',
'string',
'max:50'
],
'*.1' => [
'nullable',
'string',
'max:50'
],
'*.2' => [
'required',
'string',
'max:50'
],
'*.3' => [
'required',
'email',
'max:100',
Rule::unique('employees', 'email')->where(function ($query) {
return $query->where('company_id', Auth::user()->company_id);
})
],
'*.4' => [
'required',
'date_format:Y-m-d',
'before:' . Carbon::now()->subYears(18)->format('Y-m-d')
],
];
}
public function chunkSize(): int
{
return 1000;
}
public function transformDate($value, $format = 'Y-m-d')
{
try {
return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
} catch (\ErrorException $e) {
return \Carbon\Carbon::createFromFormat($format, $value);
}
}
}
控制器:
public function importEmployee(Request $request)
{
$validator = Validator::make($request->all(), [
'document' => 'file|mimes:xls,xlsx|max:5000',
]);
if ($request->hasFile('document'))
{
if($validator->passes()) {
$import = new EmployeeImport;
$file = $request->file('document');
$file->move(public_path('storage/employee_imports'), $file->getClientOriginalName());
Excel::import($import, public_path('storage/employee_imports/' . $file->getClientOriginalName() ));
return $this->success('Employees Imported.', [
'file' => $file
]);
}else{
return $this->error($validator->errors(), 422);
}
}
}
当我提交时,我得到了这个错误:
[2021-08-19 09:33:40] local.ERROR: Error: Call to undefined function
App\Imports\create() in
C:\xampp\htdocs\myapp\app\Imports\EmployeeImport.php:98
Stack trace:
#0 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Sheet.php(301):
App\Imports\EmployeeImport->onRow(Array)
#1 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Reader.php(114): Maatwebsite\Excel\Sheet->import(Object(App\Imports\EmployeeImport), 2)
#2
C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Concerns\ManagesTransactions.php(29): Maatwebsite\Excel\Reader->Maatwebsite\Excel{闭包}(对象(Illuminate\Database\MySqlConnection))
这是第 98 行:
$employee = create(Employee::class, $employee_data);
我该如何解决这个问题?
谢谢
更改此行
$employee = create(Employee::class, $employee_data);
到
$employee = Employee::create($employee_data);
create
不是一个单独的函数。检查这个 link
https://laravel.com/docs/8.x/eloquent#inserting-and-updating-models
我正在使用 Laravel-8 和 Maatwebsite-3.1 包进行 excel 上传:
我有这个Excelsheet(数据更感谢):
进口:
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Validators\Failure;
use Throwable;
class EmployeeImport extends DefaultValueBinder implements OnEachRow, WithStartRow, SkipsOnError, WithValidation, SkipsOnFailure, WithEvents, WithCustomValueBinder
{
// set the preferred date format
private $date_format = 'Y-m-d';
// set the columns to be formatted as
private $date_columns = ['E'];
use Importable, SkipsErrors, SkipsFailures;
public function onRow(Row $row)
{
$rowIndex = $row->getIndex();
if($rowIndex >= 1000)
return; // Not more than 1000 rows at a time
$row = $row->toArray();
$employee_data = [
'first_name' => $row[0],
'other_name' => $row[1] ?? '',
'last_name' => $row[2],
'email' => preg_replace('/\s+/', '', strtolower($row[3])),
'dob' => $this->transformDate($row[4]),
];
$employee = create(Employee::class, $employee_data);
if (User::where('email', '=', $employee->email)->exists()) {
$user = User::update([
'first_name' => $employee->first_name,
'last_name' => $employee->last_name,
'active' => 1,
]);
}else{
$user = User::create([
'email' => $employee->email,
'first_name' => $employee->first_name,
'last_name' => $employee->last_name,
'active' => 1,
]);
}
$employee->update([
'user_id' => $user->id,
]);
}
public function startRow(): int
{
return 2;
}
public function customValidationAttributes()
{
return [
'0' => 'First Name',
'1' => 'Other Name',
'2' => 'Last Name',
'3' => 'Email',
'4' => 'Date of Birth',
];
}
public function rules(): array
{
return [
'*.0' => [
'required',
'string',
'max:50'
],
'*.1' => [
'nullable',
'string',
'max:50'
],
'*.2' => [
'required',
'string',
'max:50'
],
'*.3' => [
'required',
'email',
'max:100',
Rule::unique('employees', 'email')->where(function ($query) {
return $query->where('company_id', Auth::user()->company_id);
})
],
'*.4' => [
'required',
'date_format:Y-m-d',
'before:' . Carbon::now()->subYears(18)->format('Y-m-d')
],
];
}
public function chunkSize(): int
{
return 1000;
}
public function transformDate($value, $format = 'Y-m-d')
{
try {
return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
} catch (\ErrorException $e) {
return \Carbon\Carbon::createFromFormat($format, $value);
}
}
}
控制器:
public function importEmployee(Request $request)
{
$validator = Validator::make($request->all(), [
'document' => 'file|mimes:xls,xlsx|max:5000',
]);
if ($request->hasFile('document'))
{
if($validator->passes()) {
$import = new EmployeeImport;
$file = $request->file('document');
$file->move(public_path('storage/employee_imports'), $file->getClientOriginalName());
Excel::import($import, public_path('storage/employee_imports/' . $file->getClientOriginalName() ));
return $this->success('Employees Imported.', [
'file' => $file
]);
}else{
return $this->error($validator->errors(), 422);
}
}
}
当我提交时,我得到了这个错误:
[2021-08-19 09:33:40] local.ERROR: Error: Call to undefined function
App\Imports\create() in
C:\xampp\htdocs\myapp\app\Imports\EmployeeImport.php:98
Stack trace:
#0 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Sheet.php(301):
App\Imports\EmployeeImport->onRow(Array)
#1 C:\xampp\htdocs\myapp\vendor\maatwebsite\excel\src\Reader.php(114): Maatwebsite\Excel\Sheet->import(Object(App\Imports\EmployeeImport), 2)
#2
C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Concerns\ManagesTransactions.php(29): Maatwebsite\Excel\Reader->Maatwebsite\Excel{闭包}(对象(Illuminate\Database\MySqlConnection))
这是第 98 行:
$employee = create(Employee::class, $employee_data);
我该如何解决这个问题?
谢谢
更改此行
$employee = create(Employee::class, $employee_data);
到
$employee = Employee::create($employee_data);
create
不是一个单独的函数。检查这个 link
https://laravel.com/docs/8.x/eloquent#inserting-and-updating-models