简体   繁体   中英

Cannot pull from database with laravel 5.2 running on AWS EC2 instance

I am following a video tutorial series and I'm stuck routing users to a page that displays information from a database. He's using localhost Sequel and I'm running Mysql on an AWS EC2 instance.

Here is the error

QueryException in Connection.php line 713:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'phpmyadmin.products_lists' doesn't exist (SQL: select * from 
`products_lists` where `products_lists`.`id` = 7 limit 1)

Contents of my routes.php

Route::get('product', function() {
//echo 'hello';
$product = App\products_list::find(7);
print_r($product);
});

Route::group(['middleware' => ['web']], function(){

});

contents of my model

namespace App;

use Illuminate\Database\Eloquent\Model;

class products_list extends Model
{
    //
}

and contents of my .env

APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=localhost
DB_USERNAME=root
DB_PASSWORD=snipsnip

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

notice my error says 'phpmyadmin.products_lists' but I don't see why larval insists on keeping data from my previous .env file. Also, I'm not sure about APP_KEY=SomeRandomString because, database issues aside, it runs fine with this.

Here is the content of my instance's /etc/phpmyadmin/config-db.php

$dbuser='phpmyadmin';
$dbpass='snipsnip';
$basepath='';
$dbname='phpmyadmin';
$dbserver='';
$dbport='';
$dbtype='mysql';

Laravel Eloquent tries to access table that is the plural of the model itself. That's why your classname is products_list and it tries to find a table named products_lists . If you are using custom names, then set the table name:

namespace App;

use Illuminate\Database\Eloquent\Model;

class products_list extends Model
{
    protected $table = 'your_custom_name';
}

As for the key, just generate your own:

php artisan key:generate

It turns out my issue was with the model

It should have been

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class products_list extends Model
{
    protected $table = 'products_list';
}

because Laravel, by default, is looking into the database phpmyadmin which is is not what I want. My workaround was to modify the model and create a table within phpmyadmin called products_list .

Organize your Models with PSR names ie Product ( not product_list ) once you get habituated, you will see less errors

Model Product will by default will look for a table name "products" ( plural of Model name) if you want to change it, set the protected class var $table = "your_table_name" although not recommended since if you use relationships, you have to set table name everywhere, its better to follow the convention

If your .env does not affect or update while you run php artisan key:generate probably you have incorrect permissions

set chmod 755 on .env and 755 or 777 on storage and bootstrap folders.

to check db connections working, run the migration via artisan

php artisan migrate at a basic it will create the migrations table

and if still your classes are not loaded run composer update

composer dump-auto or php artisan optimize which will compile the classes

On ubuntu systems, its better to create a new user and update the project folder with the same and not using root ( or ubuntu user on AWS EC2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM