0
votes

i'm making an app where i want to login and register and also being able to import a csv file into database , the reg and login is working good however i came accross this when i want to import the csv:

error luminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'classe' in 'where clause' (SQL: select * from accounts where (classe = 7 and nocompte = 7598) limit 1) Account table

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('AccountClass');
            $table->integer('AccountNumber');
            $table->string('AccountDesc');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('accounts');
    }
}

Account controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Accounts;
class AccountController extends Controller
{
    public function show(){
        return view ('upload');
    }
    public function store(Request $request){
        $upload = $request->file('upload-file');
        $filePath = $upload->getRealPath();
        $file = fopen($filePath,'r');
       $fileconverted =  mb_convert_encoding($file, 'UTF-16LE', 'UTF-8');
        $header = fgetcsv($file);
        // dd($header);
        $escapedHeader = [];
        foreach ($header as $key =>  $value){
            $header1=strtolower($value);
            $escapedItem=preg_replace('/[^a-z]/', '' ,$header1);

array_push($escapedHeader,$escapedItem);
        }
        while($columns=fgetcsv($file)){
            if($columns[0]==""){
                continue;
            }
           //trim data
           foreach ($columns as $key => &$value) {
            $value=preg_replace('/\D/','',$value);
        }
       $data= array_combine($escapedHeader, $columns);
    //    dd($data);
        }
        $AccountClass = $data['classe'];
        $AccountNumber = $data['nocompte'];
        $AccountDesc =  $data['libell'];
        $Accounts= Accounts::firstOrNew(['classe'=>$AccountClass,'nocompte'=>$AccountNumber]);
        $Accounts->AccountClass=$AccountClass;
        $Accounts->AccountNumber=$AccountNumber;
        $Accounts->AccountDesc=$AccountDesc;

        $Accounts->save();


    }
}
1

1 Answers

1
votes

You are telling it to query the 'accounts' table looking for a field named 'classe':

Accounts::firstOrNew(['classe'=>$AccountClass,'nocompte'=>$AccountNumber]);

Perhaps you mean 'AccountClass'?

Accounts::firstOrNew(['AccountClass' => $AccountClass, ...]);

After you adjust that you will run into the same error for 'nocompte' which also does not exist on that table ...