Ad

Laravel 5.2 - Storing Data In MySQL DB With Homestead/Virtualbox/Vagrant

- 1 answer

I am running a Vagrant/VirtualBox/Homestead local development system on my Mac machine, and I am trying to get a Laravel 5.2 project up and running. The site is loading fine, but when I try to set up data entry into the DB with forms, the data is not being recorded in my db.

As far as I can see this should work. Here is some code:

in UserController.php:

public function store() {
        $input = Request::all();

        $user = new User;
        $user->email = $input['email'];


        return redirect('/contact');
    }

Route:

Route::post('/', '[email protected]');

Form:

                <form method="POST" action="/" accept-charset="UTF-8" class="subscribe">


                    <input type="text" name="email" placeholder="Email" />

                    <ul class="buttons">
                        <li><input type="submit" value="Subscribe" class="button special"></li>
                    </ul>


                </form>  

No errors are being thrown, but the data is not being stored in my Homestead DB on my local machine. What am I doing wrong?

Ad

Answer

You are not actually inserting anything in your db. Lets assume 'MyModel' is your model, add this line MyModel::create($user);, your store function inside the controller should be something like this:

public function store() {
        $input = Request::all();

        $user = new User;
        $user->email = $input['email'];

        MyModel::create($user);

        return redirect('/contact');
    }

Cheers.

Ad
source: stackoverflow.com
Ad