I am using the Maatwebsite/excel component to import excel files. In this case, importing users. I need to validate that the mail does not exist and then import the unique data
I have this code in the import file
namespace App\Imports;
use App\User;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithValidation;
class UsersImport implements ToModel, WithValidation
{
use Importable;
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
$user = User::create([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
$user->assignRole('guest');
}
public function rules(): array
{
return [
'0' => 'required|string',
'1' => 'unique:users',
];
}
}
And this code in my controller:
public function import()
{
Excel::import(new UsersImport, request()->file('file'));
return back()->with('success', 'Importado con éxito!');
}
When I want to import duplicate data, I get the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1.1' in 'where clause' (SQL: select count(*) as aggregate from users where 1.1 = [email protected])