User Registration – Insert Data

Now we have created a registration form.

As we have defined the action of our form as user/createUser so we have to create a route for this url.

Notice the method of the route is POST as the method of our form is POST.

To insert the data we will have to create a table in database first. Lets create table users with following fields.

users table
users table


Now we have to write some code to insert the data in the users table.

Write this code to insert data


In the above code we POST the values from the form using Input::get(“fieldname”).

Then we have created an array $insertData which contains the name of the columns of users table as its indexes and their values is assigned.

Hash::make($password) is Laravel way of encrypting password. Hash is the predefined class in Laravel. You must have the length of password column in your table at least 60 to store the encrypted value.


If you fill your form and submit then you will see this error.

Mass assignment error
Mass assignment error

This is because Laravel does not allow you to save the more than one field in one query until you define them in your model. So lets do the required things in model.

If you don’t see this error then goto config/app .php and set ‘debug’ => true


If you will look to the app/models/ directory you will see that there is a model already User.php.

User model
User model

In this file you will see code like this

Here it is defined protected $table = ‘users’; this means this model will use users table from database.

Now change your code to this

You will notice that we are adding a line protected $fillable = [‘name’,’email’,‘password’];. Basically we are defining here which columns of users table are allowed to insert/update the values.

If you have more columns in the users table which are not defined in above array then you cannot insert values in them.

Now try again to submit the registration form.

You will see the error again but it will be different one.


Column error
Column error


According to this error Laravel expects you to create 2 columns in your database updated_at(datetime) and created_at(current timestamp).

But we can skip this thing also by adding a line in our model.

public $timestamps = false;

But I want to use these columns as it may be useful in future for me.

So, I am modifying users table to this

Updated users table


Now if we try to submit the registration form the data will get saved to table and you will see this

saved response
saved response


You will see a record inserted in users table.

Congrats! You have learnt now how to save data in your database in Laravel.

Next thing we will do is Login Authentication in Laravel.

Share this article: