Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Database [localhost] not configured.

I get this error message:

Database [localhost] not configured.

controllers/MenuController.php

   <?php

   namespace App\Http\Controllers;

   use App\User;
   use App\Http\Controllers\Controller;
   use DB;

   use Illuminate\Foundation\Bus\DispatchesJobs;
   use Illuminate\Routing\Controller as BaseController;
   use Illuminate\Foundation\Validation\ValidatesRequests;
   use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

   use Illuminate\Http\Request;

   class MenuController extends Controller
   {
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

   public function getMenu(Request $request)
   {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_id' => '1', 'menu_color' => $menu_color]);

    return view();
     }
  }
 ?>

views/welcome.blade.php

        <div class="content">

        <div class="title m-b-md">
            MENU CSS
        </div>

        Pick a new color for menu css:<br><br>
        <!-- <form action="{{route('menu.color')}}" method="post"> -->
        <form action="route/to/thing" method="post">
        
        
        <!-- <form method="POST" action="/posts"> -->
            {{ csrf_field() }}  
            <select name="cars">
                <option value="red">Red</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="orange">Orange</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>                  
        </div>
                        
    </div>    

routes/web.php

  Route::get('/', function () {
      return view('welcome');
  });

  Route::post('route/to/thing', 'MenuController@getMenu')->name('menu.color');

I wonder why it appears?

0 likes
28 replies
davy_yg's avatar
Level 27

Here is .env ( I thought I already configure it)

 DB_CONNECTION=localhost
 DB_HOST=127.0.0.1
 DB_PORT=3306 
 DB_DATABASE=menu_css
 DB_USERNAME=root   
 DB_PASSWORD=12345

I try to migrate: php artisan migrate

[InvalidArgumentException] Database [localhost] not configured.

fraserk's avatar
 DB_CONNECTION=localhost

should be

 DB_CONNECTION=mysql or whatever database you're using.
1 like
davy_yg's avatar
Level 27

Now, strange error appearing:

UnexpectedValueException in Response.php line 450: The Response content must be a string or object implementing __toString(), "object" given.

The program already able to insert some value into the database except that I get that error message which is annoying.

fraserk's avatar

You have to give more detail. that's a different issue to the one you posted about.

topvillas's avatar

It's not a strange error.

Your calling the view helper without giving it a path to a view.

Please do yourself a favour and watch some beginner level videos.

davy_yg's avatar
Level 27

I already did and still doing it. Here is some more details:

Controllers/MenuController.php

   <?php

   namespace App\Http\Controllers;

   use App\User;
  use App\Http\Controllers\Controller;
  use DB;

 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

 use Illuminate\Http\Request;

 class MenuController extends Controller
 {
 use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

 public function getMenu(Request $request)
{
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_color' => $menu_color]);

return view(); } }

views/welcome.blade.php

<!doctype html>
<html lang="{{ config('app.locale') }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

    
  </head>
  <body>
   

    <div class="content">
    
        <div class="content">

        <div class="title m-b-md">
            MENU CSS
        </div>

        Pick a new color for menu css:<br><br>
        <!-- <form action="{{route('menu.color')}}" method="post"> -->
        <form action="route/to/thing" method="post">
        
        
        <!-- <form method="POST" action="/posts"> -->
            {{ csrf_field() }}  
            <select name="cars">
                <option value="red">Red</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="orange">Orange</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>                  
        </div>
                        
    </div>    
        
    
 </body>
</html>
topvillas's avatar

Again ... Your calling the view helper without giving it a path to a view.

davy_yg's avatar
Level 27

menu/routes/web.php

 Route::get('/', function () {
     return view('welcome');
 }); 

 Route::post('route/to/thing', 'MenuController@getMenu')->name('menu.color');
davy_yg's avatar
Level 27

Is that not a match?

public function getMenu(Request $request)

Route::post('route/to/thing', 'MenuController@getMenu')->name('menu.color');

I have this on my router: MenuController@getMenu

topvillas's avatar

NO! I've told you what's wrong twice already.

davy_yg's avatar
Level 27

I thought I already give path to view:

views/welcome.blade.php

    <form action="route/to/thing" method="post">

Router/web.php

    Route::post('route/to/thing', 'MenuController@getMenu')->name('menu.color');
topvillas's avatar

I've lost patience now.

Look in your MenuController

return view()

You need to give it a view to return.

psaunders's avatar

@davy_yg We've all been new to this stuff before, but coming on here before you watch at least the (free) beginner videos is a dick move. It's like you're trying to drive a car without even getting your learners licence, just go get that first and then you can come in here when you're not just driving around with your hand-break on like an idiot.

davy_yg's avatar
Level 27

I think I already did. Check my post above (at the top) - already have return view() in it.

Controllers/MenuController.php

  <?php

  namespace App\Http\Controllers;

  use App\User;
  use App\Http\Controllers\Controller;
  use DB;

  use Illuminate\Foundation\Bus\DispatchesJobs;
  use Illuminate\Routing\Controller as BaseController;
  use Illuminate\Foundation\Validation\ValidatesRequests;
  use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

  use Illuminate\Http\Request;

  class MenuController extends Controller
   {
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

   public function getMenu(Request $request)
   {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_color' => $menu_color]);

   return view();
    }
  }

Yes, I watched the free video (can check my XP) - need more time to watch more. Thanks.

Snapey's avatar

You were given this advice

Again ... Your calling the view helper without giving it a path to a view.

Yet you code remains;

return view();

I think your biggest failing is not learning how to diagnose these problems for yourself. Its not just about studying the tutorials its about WORKING IT OUT look at the errors presented. Use debugging tools like dd() and even echo to work out how far you have got in your code.

Stop keep moving one error to the next and just think, I'll just paste that into the forum.

1 like
davy_yg's avatar
Level 27

Nevermind, I think I am still do not know anything about laravel until next year. Well, I still cannot figure out what's wrong.

davy_yg's avatar
Level 27

Hey I think I am getting there! Check this code out. I already able to store the database yet do not know how to print it out on another page:

Controllers/MenuController.php

  class MenuController extends Controller
  {
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

 public function getMenu(Request $request)
  {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_color' => $menu_color]);

  return view('/print', 'MenuController@showResult');
  }

public function showResult()
{
    
$menu_color = $request->get('cars');    
    
$menu_color = User::where('menu_color', $menu_color)->first();  
    
return view('/print', ['menu_color' => $menu_color]);   
}
 }

views/print.blade.php

This is the end result:

I wonder why the error appearing: 1 ErrorException in Factory.php line 134: array_merge(): Argument #2 is not an array

Cronix's avatar

get rid of the / in /print, assuming your actual view file is at /resources/views/print.blade.php. Not sure why you're adding a slash...

Also, this is not correct: return view('/print', 'MenuController@showResult');

You can't do that... the 2nd parameter to view() should be an array of data to pass to the view (if it's needed), not a controller!

It would help if you at least read the docs for what you are trying to do: https://laravel.com/docs/5.4/views

1 like
davy_yg's avatar
Level 27

I fix some stuff and get:

ErrorException in bd4cb01e3f3da8a9060df7c7c8761cd6e195eefb.php line 7: Parse error: syntax error, unexpected '}' (View: C:\xampp\htdocs\menu\resources\views\print.blade.php)

print.blade.php

  <html>

  <body>

  This is the end result:  <?php {{ $menu_color }} ?>

  </body>

  </html>

Controllers/MenuController.php

  class MenuController extends Controller
  {
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

   public function getMenu(Request $request)
   {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_color' => $menu_color]);

   return view('print', ['menu_color' => $menu_color]);
    }

public function showResult()
{
    
$menu_color = DB::table('menu')->where('menu_color', $menu_color)->first(); 
        
return view('print', ['menu_color' => $menu_color]);    
}
  }
m7vm7v's avatar

When you use {{ $menu }} there is NO need of

<?php ?>
{{ $var }} means <?php echo $var; ?>
davy_yg's avatar
Level 27

Now, TokenMismatchException in VerifyCsrfToken.php line 68:

I wonder why do I need token?

davy_yg's avatar
Level 27

Looks like this is works!

 <html>

 <body>

 This is the end result:  {{ $menu_color }} 

{{ csrf_field() }} 

</body>

</html>

I try this one that does not work out yet:

  class MenuController extends Controller
  {
  use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

  public function getMenu(Request $request)
   {
   $menu_color = $request->get('cars');

   DB::table('menu')->insert(['menu_color' => $menu_color]);

    return view('print', ['menu_color' => $menu_color]);
     }

public function showResult()
{
    
 //$menu_color = DB::table('menu')->where('menu_color',    $menu_color)->first();

   $menu_colors = DB::table('menu')->get();  
        
return view('print', ['menu_colors' => $menu_colors]);  
}
  }


   <html>

   <body>

  This is the end result:  {{ $menu_color }} 

  {{ csrf_field() }} 

 foreach ($menu_colors as $menu_colors) {
      echo $menu_colors->menu_colors;
 }

 </body>

 </html>

It printed the text instead of the result.

tykus's avatar

Awesome! Delighted for you!

3 likes
BHiko's avatar

In my case php artisan config:clear solved the issue

Please or to participate in this conversation.