2

I am working on a new laravel 5.5 site and am getting an exception I don't understand when the application tries to connect to a Postgresql database.

All settings in my env file are correct for the database connection

I am creating the table with a migration and when I run php artisan migrate the table gets created.

Under /app I have a file NbnCo.php that contains

class NbnCo extends Model
{

    protected $fillable=[

// All the db table fields are here

];
}

Under /app/Http/Controllers I have NbnCoController.php

It contains

namespace App\Http\Controllers;

use App\NbnCo;
use Illuminate\Http\Request;

use App\Http\Requests;

class NbnCoController extends Controller
{
    public function showForm()
   {
        return view('upload');
   }

public function store(Request $request)
{   
    //get file
    $upload=$request->file('upload-file');

    $filePath=$upload->getRealPath();

    //open and read
    $file=fopen($filePath, 'r');

    $header= fgetcsv($file);

    $processedHeader=[];
    //validate
    foreach ($header as $key => $value) {
        $lheader=strtolower($value);
        array_push($processedHeader, $lheader);
    }

    while($columns=fgetcsv($file))
    {
        if($columns[0]=="")
        {
            continue;
        }

        $data=array_combine($processedHeader, $columns);

        // Table update
        $nbn_location_identifier=$data['nbn_location_identifier'];
        // the rest of the folumns follow the same style

        $nbn = NbnCo::firstOrNew(['nbn_location_identifier'=>$nbn_location_identifier]);
        // the rest are added in the same way

        $nbn->save();
     }

   }
}

The idea is that a simple form is displayed and a CSV file is selected to be uploaded into the nbnco table in the nbn database.

I am getting 2 PDO errors

PDOException in Connection.php line 337:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nbn_cos" does not 
exist
LINE 1: select * from "nbn_cos" where ("nbn_location_identifier" = $...
^

and

QueryException in Connection.php line 770:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "nbn_cos" does not 
exist
LINE 1: select * from "nbn_cos" where ("nbn_location_identifier" = $...
^ (SQL: select * from "nbn_cos" where ("nbn_location_identifier" = 
LOC000012257222) limit 1)

I do not understand where the nbn_cos table that laravel is trying to connect to is coming from. I also don't understand where the nbn_cos relation is coming from.

The best I can find is that I am doing something wrong with the naming of my classes and the name of the database table. I can't work out, even with a lot of Google searching, where the database table name is actually passed to the connection and SQL statements.

1 Answer 1

2

Laravel and Postgres requires the table to be included in the model, and the table name.

protected $table = 'database.table';

So, if your database is called test and the table is nbnco, it will look something like:

protected $table = 'test.nbnco'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.