Rob Bauman
Rob Bauman

Reputation: 65

Laravel Eloquent Create does nothing

So I'm learning Laravel, and I'm trying to create a new user in the database using data entered into a form. In my controller, I'm getting the form data fine, passing it to a validator with some rules which pass fine, but then when I try to create the User, nothing gets added to the database and it redirects to the basic "Whoops, looks like something went wrong." error page instead of the page I'm telling it to.

Here's my controller function:

public function doRegister() {
    $rules = array(
        'fname'=>'required|alpha|min:2',
        'lname'=>'required|alpha|min:2',
        'email'=>'required|email|unique:users',
        'company'=>'required|alpha|min:2',
        'password'=>'required|alpha_num|between:6,12|confirmed',
        'password_confirmation'=>'required|alpha_num|between:6,12'
    );

    $validator = Validator::make(Input::all(), $rules);

    if($validator->passes()) {
        User::create(array(
            'fname'      => Input::get('fname'),
            'lname'      => Input::get('lname'),
            'email'      => Input::get('email'),
            'password'   => Hash.make(Input::get('password')),
            'company'    => Input::get('company'),
            'created_at' => date('Y-m-d H:m:s'),
            'updated_at' => date('Y-m-d H:m:s')
        ));

        return Redirect::to('login')->with('message', 'Thank you for registering!');
    } else {
        return Redirect::to('register')
            ->with('message', 'The following errors occurred')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }
}

Removing the User::create() section, the redirect works perfectly fine. Just to start with I've included all the database fields in the fillable array in my User model. Still doesn't work. Any ideas?

Upvotes: 1

Views: 867

Answers (1)

baao
baao

Reputation: 73241

Not the direct answer to your question, but the quickest way for you to find it.

Open app.php

enter image description here

edit the last line seen in this picture from (in your case) false to true:

enter image description here

Once done that, laravel will tell you what the error is.


One error for sure is

'password'   => Hash.make(Input::get('password')),

should be with ::

'password'   => Hash::make(Input::get('password')),

You don't need this:

'created_at' => date('Y-m-d H:m:s'),
'updated_at' => date('Y-m-d H:m:s')

laravel will do this for you!

If it doesn't work after this, try to run

php artisan dump-autoload 

from your terminal to generate an optimized class loader!

Upvotes: 1

Related Questions