问题
I am new to Laravel and am getting an error when trying to run a simple command in the controller Article::all()
. The error is:
PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
Here is my .env
file:
APP_ENV=local
APP_DEBUG=true
APP_KEY=v1xavEadi4rHv0EGn05zQvtVAtQRA9zo
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
The controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Article;
class ArticlesController extends Controller
{
public function index()
{
$articles = Article::all();
}
}
and the model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $fillable = [
'name',
'body'
];
}
回答1:
Anyone else looking at this in the future with the same issue - restart the command prompt after changing the env file in windows. Restarting the server didn't work, but closing out of everything and then trying it again worked.
回答2:
If you're using Homestead with the default database credentials, then you have to edit your .env
file:
DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=homestead
DB_PASSWORD=secret
Though it's recommended to change the default password.
来源:https://stackoverflow.com/questions/33973442/laravel-database-access-denied