I'm trying to implementing an import csv file. When i tried to upload a csv file, it doesn't save the value into the database, and when i dd() it, it doesn't show anything. How can i fix this issue??
The vendor import model :
<?php
namespace Modules\User\Imports;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Spatie\Permission\Models\Role;
class VendorImport implements ToModel, WithCalculatedFormulas, WithCustomCsvSettings
{
/**
* @param Collection $collection
*/
public function getCsvSettings(): array
{
return [
'delimiter' => ';'
];
}
public function model(array $row)
{
// Implement your import logic here
dd($row);
$user = new User([
'name' => $row[0],
'first_name' => $row[1],
'last_name' => $row[2],
'email'=> $row[3],
'email_verified_at'=> Carbon::now()->timestamp,
'password'=> $row[5],
'address'=> $row[6],
'address2'=> $row[7],
'phone'=> $row[8],
'phone2'=> $row[9],
'birthday'=> $row[10],
'city'=> $row[11],
'state'=> $row[12],
'country'=> $row[13],
'zip_code'=> $row[14],
'last_login_at'=> $row[15],
'avatar_id'=> $row[16],
'bio'=> $row[17],
'status'=> $row[18],
'create_user'=> $row[19],
'update_user'=> $row[20],
'vendor_commission_amount'=> $row[21],
'vendor_commission_type'=> $row[22],
'deleted_at'=> null,
'remember_token'=> $row[24],
'created_at'=> Carbon::now()->timestamp,
'updated_at'=> Carbon::now()->timestamp,
'payment_gateway'=> $row[27],
'total_guests'=> $row[28],
'locale'=> $row[29],
'business_name'=> $row[30],
'avatar'=> $row[31],
'messenger_color'=> $row[32],
'dark_mode'=> $row[33],
'active_status'=> $row[34],
'verify_submit_status'=> $row[35],
'is_verified'=> $row[36],
'user_name'=> $row[37],
'member'=> $row[38],
'role'=> $row[39],
]);
$user->save(); // Corrected method call
if ($role = Role::findById(1)) {
$user->syncRoles([$role]);
}
return $user;
}
}
The controller :
public function import(Request $request)
{
$this->validate($request, [
'file' => 'required|mimes:csv'
]);
$file = $request->file('file');
$nama_file = rand() . $file->getClientOriginalName();
$file->move('file_vendor', $nama_file);
Excel::import(new VendorImport, public_path('/file_vendor/' . $nama_file));
return redirect()->back()->with('success', __('Upload successfully!'));
}