Nicho's avatar
Level 1

Import csv file failed

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!'));
    }
0 likes
6 replies
Nakov's avatar

And there is no error? I would guess that the public_path is the issue, because the file should go in the storage local files so unless you have ran php artisan storage:link before, those are not really available in the public folder.. you could try this:

(new VendorImport)->import('file_vendor/' . $nama_file, null, \Maatwebsite\Excel\Excel::CSV);

Make sure you add : use Importable; in your VendorImport file. Like here: https://docs.laravel-excel.com/3.1/imports/importables.html

Nicho's avatar
Level 1

@Nakov It's still won't save the import file, and when i tried to dd($row) it doesn't show anything, doesn't show the array, and no errors

Nakov's avatar

@Nicho instead of using move try using storeAs because you can move an already stored file, and you haven't done that in order to be moving something..

so try this:

$file = $request->file('file');
$nama_file = rand() . $file->getClientOriginalName();
$path = $file->storeAs('file_vendor', $nama_file);

(new VendorImport)->import($path, null, \Maatwebsite\Excel\Excel::CSV);
Nicho's avatar
Level 1

@Nakov Um.... it still won't import the data Is it my route that's wrong. Here's the route :

Route::get('/import', 'UserController@import_form')->name('user.admin.import.form');
Route::post('/import', 'UserController@import')->name('user.admin.import');
Nakov's avatar

@Nicho You can easily check that by adding dd() within your controller, and see if when you post you see the debug message.

Also make sure you add enctype="multipart/form-data" to your <form> in the blade view, that's used for letting the server know that you are uploading a file.

Snapey's avatar

does the uploaded file appear in your public folder?

Please or to participate in this conversation.