0

I'm on a Laravel 8 project using the following line in one of my controllers to grab part of a table out of a database:

$post = \DB::table('posts')->where('slug', $slug)->first();

But it gives me this error:

Illuminate\Database\QueryException SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. (SQL: select * from posts where slug = my-first-post limit 1)

Here's my database connection in my .env file for reference, everything seems correct:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_HOST=mysql

DB_PORT=3306

DB_DATABASE=cascade

DB_USERNAME=root

DB_PASSWORD=

What could be wrong here?

dellc01
  • 9
  • 1
  • 1
    The message means that PHP can't resolve the address of the database server. You have two entries for `DB_HOST`. It's likely that the second is wrong and should be removed. –  Dec 11 '20 at 01:49
  • 1
    Thank you, that worked. That was a bit confusing since it was written that way on install and I haven't had this issue before but it's good to know from now on. – dellc01 Dec 11 '20 at 02:10
  • Also, check the value for the `slug` variable it should have quotes. – laviku Dec 11 '20 at 02:41

0 Answers0