skip to Main Content

I want to import excel, and my code of controller is

$path = $request->file('file')->getRealPath();
$dataOfMember = Excel::toArray(new InterfaceMember(), $path);
              
$dataOfMember = $dataOfMember[0];
$insert_dataOfMember = [];
          
foreach ($dataOfMember as $row) {
    $insert_dataOfMember[] = [
        'kodeToko' => $kodeToko = session('authData')['kodeToko'],
        'idMember' => DB::select(" select dbo.fcIdMember(".$this->kodeToko.", '".$row[1]."')"),
        'RefereceID' => $row[0],
        'KodeMember' => $row[1],
        'NamaMember' => $row[2], 
        'JenisIdentitas' => $row[3], 
        'NomorIdentitas' => $row[4], 
        'AlamatMember' => $row[5], 
        'KodePos' => $row[6], 
        'JenisKelamin' => $row[7], 
        'TglLahir' => $row[8], 
        'NoHp' => $row[9],
        'PointReedem' => $row[10],
        'RpMember' => $row[11],
    ];
}
          
if (!empty($insert_dataOfMember)) {
    DB::table('uplMemberDeposite')->insert($insert_dataOfMember);
}
          
return back()->with('success', 'Excel Data Imported successfully.');

when i running mya code, i have error that ""Array to string conversion". What should i do to slove this error? anyone please help me!

2

Answers


  1. I think the problem lies with the line

    'idMember' => DB::select("select dbo.fcIdMember(".$this->kodeToko.", '".$row[1]."')"),
    

    DB::select returns an array according to the documentation.

    If you did dd(DB::select("select dbo.fcIdMember(".$this->kodeToko.", '".$row[1]."')"));, I think you’d see the following on your screen:

    array:1 [
      0 => {
        +"columnName": columnValue
      }
    ]
    

    That’s the array that is giving you the error. You’re passing that entire array when what you’re really trying to pass is the columnValue part.


    foreach ($dataOfMember as $row) {
        $result = DB::select(" select dbo.fcIdMember(".$this->kodeToko.", '".$row[1]."')");
    
        $insert_dataOfMember[] = [
            'kodeToko' => $kodeToko = session('authData')['kodeToko'],
            'idMember' => $result[0]->columnName, 
            'RefereceID' => $row[0],
            'KodeMember' => $row[1],
            'NamaMember' => $row[2], 
            'JenisIdentitas' => $row[3], 
            'NomorIdentitas' => $row[4], 
            'AlamatMember' => $row[5], 
            'KodePos' => $row[6], 
            'JenisKelamin' => $row[7], 
            'TglLahir' => $row[8], 
            'NoHp' => $row[9],
            'PointReedem' => $row[10],
            'RpMember' => $row[11],
        ];
    }
    
    Login or Signup to reply.
  2. Your query is correct. You just do a more small step:

    $idMembers = DB::select("Select dbo.fcIdMember(".$this->kodeToko.", '".$row[1]."') as idMember");
    
    $idMember = count($idMembers) ? $idMembers[0]->idMember : null;
    
    //....  
    $insert_dataOfMember[] = [
        //....     
       'idMember' => $idMember,
        //....     
    ];
    //....  
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search