I know a similar question has been asked but the reasons for those error does not imply in my case because I am not fetching results anywhere in code.
I am not using the 'find' or 'findOrFail' method anywhere in code.
I am getting this error:
No query results for model [App\User] NULL 387 /laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
My Excel import code:
<?php
namespace App\Imports;
use Auth;
use App\User;
use App\EmployeePerformance;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class HierarchyImport implements ToCollection, WithHeadingRow {
public function collection(Collection $rows) {
foreach ($rows as $row) {
$user = $this->createUserNew($row, $row['role'], $row['parent_id']);
}
}
/**
* Transform a date value into a Carbon object.
*
* @return \Carbon\Carbon|null
*/
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 createUserNew($row, $role, $parent_id){
$emp_id = $row['employee_id'];
$name = $row['employee_name'];
$email = $row['email'];
$user = new User;
$user->company_id = 1;
$user->employee_id = $emp_id;
$user->parent_id = $parent_id;
$user->title = $row['title'];
$user->region_number = 2;
$user->name = trim(preg_replace('/[^A-Za-z0-9\-]/', ' ', $name));
$user->email = $email;
$user->email_verified_at = null;
$user->gender = ($role == 'employee') ? $row['gender'] : null;
$user->hire_date = ($role == 'employee') ? $this->transformDate($row['hire_date']) : null;
$user->date_of_birth = null;
$user->age = ($role == 'employee') ? $row['dob'] : null;
$user->password = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'; // password
$user->employee_status = 0;
$user->save();
if($user)
//$user = $user->assignRole($role);
return $user;
}
}
In my controller:
$import = new HierarchyImport;
$importExcel = Excel::import($import, $request->file('file'));
PS:
I tried User::create($user);
but the same result, and the create method is returning the No query results for the model.