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

davy_yg's avatar
Level 27

Connecting database with view

Hello,

I am trying to make this code works. If someone choose red for instance. I expect the database would store red in the menu_color. How to make that works?

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="/action_page.php">
                <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>

Controllers/controller.php

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

public function getMenu()
{

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

return view();
}

}

0 likes
31 replies
davy_yg's avatar
Level 27

What should I replace that with so that it stores the value that I select into the database?

tykus's avatar

Keep it simple for now; your form will post to a menus URI:

<div class="content">
    <div class="title m-b-md">
        MENU CSS
    </div>
    Pick a new color for menu css:<br><br>
    <form action="menus" method="POST">
        {{ 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>
        <input type="submit" value="Submit">
    </form>
</div>

Add a route to routes/web.php to receive the form submission:

Route::post('menus', function () {
    DB::table('menu')->insert([
        'menu_id' => '1',
        'menu_color' => request()->get('cars')
    ]);

    return back();
});
davy_yg's avatar
Level 27

Maybe this is the answer:

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

            Pick a new color for menu css:<br><br>
            <form action="{{action('MenuController@getMenu')}}">
                <select name="cars">
                    <option value="green">Green</option>
                    <option value="blue">Blue</option>
                    <option value="black">Black</option>
                    <option value="orange">Orange</option>
                    <option value="red">Red</option>
                </select>
            <br><br>
            <input type="submit" value="Submit">
            </form>

                            
        </div>

How to write the Routes so that it works well?

There is this error right now:

ErrorException in UrlGenerator.php line 337: Action App\Http\Controllers\MenuController@getMenu not defined. (View: C:\xampp\htdocs\menu\resources\views\welcome.blade.php)

davy_yg's avatar
Level 27

@tykus : it could insert red, blue, green, orange. How to determine which is which to insert - if the one selected is the one besides red?

tykus's avatar

Do you have a app/Html/Controllers/MenuController.php with a class called MenuController containing a getMenu() method? You call your controller Controllers/controller.php in the OP!

EDIT: updated the previous answer to user request() helper to get the selected color from the request.

tykus's avatar

OP == original post or original poster

Whatever I suggested in the route closure can be moved to the Controller method instead if that's your preference:

class MenuController extends Controller
{
    public function getMenu()
    {
        DB::table('menu')->insert([
            'menu_id' => '1',
            'menu_color' => request()->get('cars')
            ]);

        return back();
    }
}

You will also see examples where the Request object is injected into the method:

class MenuController extends Controller
{
    public function getMenu(Request $request)
    {
        DB::table('menu')->insert([
            'menu_id' => '1',
            'menu_color' => $request->get('cars')
            ]);

        return back();
    }
}

Either is fine.

davy_yg's avatar
Level 27

I copy your code. and I get this error message:

Whoops, looks like something went wrong.

1/1 InvalidArgumentException in DatabaseManager.php line 137: Database [localhost] not configured.

davy_yg's avatar
Level 27

routes/web.php

 Route::post('menus', function () {
     DB::table('menu')->insert([
         'menu_id' => '1',
         'menu_color' => request()->get('cars')
     ]);

  return back();
  }); 

Controllers/controller.php

    class MenuController extends Controller
   {
      public function getMenu(Request $request)
      {
        DB::table('menu')->insert([
            'menu_id' => '1',
            'menu_color' => $request->get('cars')
        ]);

     return back();
     }
   }

views/welcome.blade.php

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

        Pick a new color for menu css:<br><br>
        <form action="{{action('MenuController@getMenu')}}">
            <select name="cars">
                <option value="green">Green</option>
                <option value="blue">Blue</option>
                <option value="black">Black</option>
                <option value="orange">Orange</option>
                <option value="red">Red</option>
            </select>
        <br><br>
        <input type="submit" value="Submit">
        </form>

ErrorException in UrlGenerator.php line 337: Action App\Http\Controllers\MenuController@getMenu not defined. (View: C:\xampp\htdocs\menu\resources\views\welcome.blade.php)

tykus's avatar

That database error is related to your local configuration.

You're going to need to get yourself up to speed on some of the basic concepts - there are free Laravel From Scratch courses here on Laracasts.

Essentially if you choose this action() helper:

<form action="{{action('MenuController@getMenu')}}">

then you do not need the route definition (and closure) at all; so you can remove that from routes/web.php completely.

You appear to have defined your MenuController inside the existing app/Http/Controller.php file? You must create a MenuController.php file for this; from your terminal php artisan make:controller MenuController

Move the getMenu() method in there

AddWebContribution's avatar

I think you should use below code for example.

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">
        <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>

Create route in route.php file

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

Controllers/controller.php

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

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

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

   return view();
    }
}

Hope this code work for you !

davy_yg's avatar
Level 27

FatalErrorException in Controller.php line 10: Class 'App\Http\Controllers\Controller' not found

menu/app/Http/Controllers/Controller.php

  1. in Controller.php line 10

Snapey's avatar

Give it up guys.

I think previous coding experience is excel spreadsheet

2 likes
davy_yg's avatar
Level 27

Gee, I am quite new in laravel lol. I only know CI for a year and just move to laravel.

tykus's avatar

@davy_yg you have been given answers that will solve your problem, but it seems that you are determined to work against those answers. What have your done to cause this error:

FatalErrorException in Controller.php line 10: Class 'App\Http\Controllers\Controller' not found menu/app/Http/Controllers/Controller.php in Controller.php line 10

Seems like you have deleted the Controller.php file which contains the parent Controller class that is extended by every Controller you app will need. Why would you do that; you were told that a separate MenuController was required?

davy_yg's avatar
Level 27

No, I do not delete controller.php - it still there!

menu/app/Http/Controllers/Controller.php

  <?php

  namespace App\Http\Controllers;

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

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

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

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

   return view();
  }
 }

 ?>

menu/app/Http/Controllers/MenuController.php

    <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

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

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

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

   return view();
   }
 }
tykus's avatar

Dude!??!

You have renamed the class to MenuController in Controller.php, can you please put it back to it's original state (yes, the even means without the closing ?> tag!!!)

// menu/app/Http/Controllers/Controller.php

<?php

namespace App\Http\Controllers;

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

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

davy_yg's avatar
Level 27

Now,

MethodNotAllowedHttpException in RouteCollection.php line 251:

Snapey's avatar

Gee, I am quite new in laravel lol.

Which is why on previous posts I have suggested that you forget trying to modify this existing (badly designed) project until you know what you are doing. Follow some simple tutorials and build up your skills.

I only know CI for a year and just move to laravel.

You appear to not know how PHP classes work so I guess that must have been simple procedural code.

Dude, you are way out of your depth at the moment.

@tykus spend your time negotiating a deal between north and south korea - you are more likely to get a positive result.

1 like
davy_yg's avatar
Level 27

menu/app/Http/Controllers/Controller.php

     <?php

     namespace App\Http\Controllers;

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

     use Illuminate\Http\Request;

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

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

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

     return view();
     }
    }

   ?>

menu/app/Http/Controllers/MenuController.php

// menu/app/Http/Controllers/Controller.php

     <?php

     namespace App\Http\Controllers;

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

    class Controller extends BaseController
    {
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
     }

FatalErrorException in Controller.php line 4: Namespace declaration statement has to be the very first statement or after any declare call in the script

in Controller.php line 4

Line 4: namespace App\Http\Controllers;

Why does it mention to be place on the very first statement while I already did place it on the very first code line:

namespace App\Http\Controllers;

I also cannot find: use Illuminate\Http\Request; (file)

I try to comment that out and still get another error. So I am confuse. I do not have much time to catch up please help fixing the error and I will learn in the next lesson. I have to do this for my job and have no choice. I still watch the tutorial though and hoping that I can catch up.

MaverickChan's avatar

@Snapey

Told you guys not to waste time on helping this jerk , he showed no respect to everyone including himself

davy_yg's avatar
Level 27

@MaverickChan: stop commenting on my post!! Everyone has to go through this fase before getting better!

MaverickChan's avatar

i can comment whatever i want , and i want everyone know who you are.

Everyone will feel the same way after browsing all your post.

you never listen , never learn

davy_yg's avatar
Level 27

It does not matter to me. I have to go through this to get better! I also in the middle of learning all the tutorial. You can see my XP keeps adding.

Yet, I need help in the tutorials since no one else can help me except myself and this forum for now. No choice. I also paid for the forum! I think that's find for me to ask for help.

If you don't like it just ignore it.

davy_yg's avatar
Level 27

1/1 MethodNotAllowedHttpException in RouteCollection.php line 251:

routes/web.php

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

I also wonder why method not allowed? I already write getMenu as the method.

Nash's avatar

MethodNotAllowedHttpException means that the HTTP method of the request is different than what the application is expecting for that route, for example, doing a GET request when you're supposed to do a POST request.

We've all been beginners at some point and that's totally fine... everyone has to start somewhere, right? There is, however, a small learning curve and some prerequisites to using something like Laravel.

To understand Laravel, you must first have a basic understanding of Object-Oriented programming and the Model-View-Controller architectural pattern (and of course, PHP, SQL and HTML). If you do not have understand these things first, you are going to run into problem after problem, but without the ability to understand where to begin solving those problems.

I would recommend going through the following series in the following order.

  1. The PHP Practitioner: https://laracasts.com/series/php-for-beginners

  2. Object-Oriented Bootcamp: https://laracasts.com/series/object-oriented-bootcamp-in-php

  3. Laravel 5.4 From Scratch: https://laracasts.com/series/laravel-from-scratch-2017

davy_yg's avatar
Level 27

I have gone through number 3) and have some knowledge of PHP

I will consider number 2)

I search the forum for similar error. Both my view and routes uses post method now and now I get this error:

1/1 FatalErrorException in MenuController.php line 12: Trait 'App\Http\Controllers\AuthorizesRequests' not found

in MenuController.php line 12

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

line 12 : use AuthorizesRequests

I try to search the forum for similar error and cannot find it.

davy_yg's avatar
Level 27

Nevermind, I find solution to this one and think to open new post for new errors. Thanks !

MaverickChan's avatar

@Nash

See ? He never want to learn laravel. you are wasting your time. Please don't reply him anymore.

Next

Please or to participate in this conversation.