Skip to content
Advertisement

No query results for model [AppUser] NULL 387 /laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

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 [AppUser] NULL 387 /laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

My Excel import code:

<?php

namespace AppImports;
use Auth;
use AppUser;
use AppEmployeePerformance;
use IlluminateSupportCollection;
use MaatwebsiteExcelConcernsToCollection;
use MaatwebsiteExcelConcernsWithHeadingRow;
use IlluminateSupportFacadesValidator;
use IlluminateValidationRule;

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 CarbonCarbon|null
     */
    public function transformDate($value, $format = 'Y-m-d'){
        try {
            return CarbonCarbon::instance(PhpOfficePhpSpreadsheetSharedDate::excelToDateTimeObject($value));
        } catch (ErrorException $e) {
            return CarbonCarbon::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.

Advertisement

Answer

So I found out that some of the fields in the excel file are null, and when I was trying to insert them, it was giving the strange error No query results for model.

This has been fixed by handling the null values before inserting.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement