Tutorials , code & Inspiration

Create a CRUD Application with Laravel 5.2


let’s create a simple CRUD application with Laravel . we have updated This tutorial to laravel 5.2 . The application we want to create will manage the users of our application. We will create the following list of features for our application:

  • List users (read users from the database)
  • Create new users
  • Edit user information
  • Delete user information
  • Adding pagination to the list of users

Now to start off with things, we would need to set up a database. So if you have phpMyAdmin installed with your local web server setup, head over to http://localhost/phpmyadmin; if you don’t have phpMyAdmin installed, use the MySQL admin tool workbench to connect with your database and create a new database.

Requirements -> html form –> please install this and follow instructions from official site




Now we need to configure Laravel to connect with our database. So head over to your Laravel application folder, open config/database.php, change the MySQL array, and match your current database settings. Here is the MySQL database array from database.php file:

    'mysql' => array(
      'driver'    => 'mysql',
      'host'      => 'localhost',
      'database'  => '<yourdbname>',
      'username'  => 'root',
      'password'  => '<yourmysqlpassord>',
      'charset'   => 'utf8',
      'collation' => 'utf8_unicode_ci',
      'prefix'    => '',

Now we are ready to work with the database in our application. Let’s first create the database table Users via the following SQL queries from phpMyAdmin or any MySQL database admin tool;

  'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
  'username' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'password' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'email' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'phone' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'name' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'created_at' timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  'updated_at' timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY ('id')

Now let’s seed some data into the Users table so when we fetch the users we won’t get empty results. Run the following queries into your database admin tool:

INSERT INTO 'users' ('id', 'username', 'password', 'email', 'phone', 'name', 'created_at', 'updated_at') VALUES
(1, 'john', 'johndoe', '[email protected]', '123456', 'John', '2013-06-07 08:13:28', '2013-06-07 08:13:28'),
(2, 'amy', 'amy.deg', '[email protected]', '1234567', 'amy', '2013-06-07 08:14:49', '2013-06-07 08:14:49');


Later we will see how we can manage our database via laravel 5.2’s powerful migrations features. At the end of this chapter, I will introduce you to why it’s not a good practice to manually create SQL queries and make changes to the database structure. And I know that passwords should not be plain too!


Listing the users – read users from database

Let’s read users from the database. We would need to follow the steps described to read users from database:

  • A route that will lead to our page
  • A controller that will handle our method
  • The Eloquent Model that will connect to the database
  • A view that will display our records in the template

So let’s create our route at /app/http/routes.php. Add the following line to the routes.php file:

Route::resource('users', 'UserController');

If you have noticed previously, we had Route::get for displaying our page Controller. But now we are using resource. So what’s the difference?

In general we face two types of requests during web projects: GET and POST. We generally use these HTTP request types to manipulate our pages, that is, you will check whether the page has any POST variables set; if not, you will display the user form to enter data. As a user submits the form, it will send a POST request as we generally define the <form method="post"> tag in our pages. Now based on page’s request type, we set the code to perform actions such as inserting user data into our database or filtering records.

What Laravel provides us is that we can simply tap into either a GET or POST request via routes and send it to the appropriate method. Here is an example for that:

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

See the difference here is we are registering the same URL, /register, but we are defining its GET method so Laravel can call UserController class’ showUserRegistration method. If it’s the POST method, Laravel should call the saveUser method of the UserController class.

You might be wondering what’s the benefit of it? Well six months later if you want to know how something’s happening in your app, you can just check out the routes.php file and guess which Controller and which method of Controller handles the part you are interested in, developing it further or solving some bug. Even some other developer who is not used to your project will be able to understand how things work and can easily help move your project. This is because he would be able to somewhat understand the structure of your application by checking routes.php.

Now imagine the routes you will need for editing, deleting, or displaying a user. Resource Controller will save you from this trouble. A single line of route will map multiple restful actions with our resource Controller. It will automatically map the following actions with HTTP verbs:

prevChapter 1 of 6

  1. Ibn Shajahan says

    nice tutorial

    1. Deven Rathore says


  2. Randy says

    Great tutorial! I have a question regarding the steps on page 8. You say that you need to create a view file in the directory /app/views/users/index.blade.php, as the statement would return the view file from users.index.
    But in other tutorials and explanations on this website says that the blade files need to go in the directory file at: /resources/views/ ? In my directory i cant find any /app/views/ directory. So am i doing something wrong here?

    1. Deven Rathore says

      you have to create file under /resources/views/ thanks for pointing that out actually it was directory structure of laravel 4 but i have corrected it ! thanks for contributing

  3. mojoblanco says

    Wow .. This is a great tutorial. Thanks for this …. Good job!

    1. Deven Rathore says


  4. Tully says

    How is this Laravel 5.2? It’s written using Laravel 4 functionality. This won’t work with Laravel 5. For example, there’s no Form package in Laravel 5.

    1. Deven Rathore says

      Requirements -> html form –> please install this and follow instructions from official site .. please see above

      1. Tully says


        class UserController extends BaseController
        as well. In Laravel 5.2, it’s Controller, not BaseController, that artisan make:controller creates. Just suggesting to do a pass over it as it’s obviously a Laravel 4 article that has had the title updated but the code should be reviewed as well.

        1. WillyWonker says

          You’re right, the code doesn’t even work for 5.2 without realizing it’s L4. And when posting code, you should do complete files, not snippets lacking the correct namespace ‘use statements’

  5. mokalovesoulmate says

    Page #2 correction for sql command:

    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `phone` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
    `updated_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

    1. Sagar Chapagain says

      Have you found the solution? In my case the syntax error disappears after removing single quotes placed for table_name and column_name.

  6. Dale Pinn says

    Hey Deven
    I am a little stuck on page 6 here. You are saying I need to go to the User.php file, which I did..
    Do I replace what is in the file? ie Take out the entire class already there?

    User extended Authenticatable

    And then add

    User extends Eloquent

    Do I also place the next two commands above
    $users = User:all(); etc within the 2 curly brackets?

    Otherwise, I am thoroughly enjoying this tutorial. ;-)

    1. Deven Rathore says

      yeah .. replace user.php file

  7. Jessé Leal dos Santos says

    Thanks! Nice tutorial

    1. Deven Rathore says

      you are welcome

  8. Pramod Sah says

    Wow .. This is a great tutorial for laravel beginners.. Good work Deven.

    1. Deven Rathore says

      thankuuh . dont forget to share it

  9. Lucas Ting says

    I am a little stuck on page 6 here.
    I got an error.
    FatalErrorException in User.php line 9:
    syntax error, unexpected ‘$users’ (T_VARIABLE), expecting function (T_FUNCTION)

  10. J.V Krakowski says

    Could you make it 1 page? This setup is harder to read.

  11. [email protected] says

    I’ve got an error:

    FatalThrowableError in UserController.php line 37:
    Class ‘AppHttpControllersInput’ not found

    and when i change the $input = User::all(), I get a new error:
    FatalThrowableError in UserController.php line 38:
    Class ‘AppHttpControllersValidator’ not found

    how can i solve this? thanks

    1. Deven Rathore says

      use AppUser; or Or you can change your controller code to be AppUser::create(… (notice the at the beginning)

      1. [email protected] says

        ok2, tnx for the response. I solve it by importing this classes:

        use AppUser;
        use IlluminateSupportFacadesInput;
        use IlluminateSupportFacadesRedirect;
        use Validator;
        use View;

        but when I create new user, the username, password and phone will not get a data to store in the DB as well as updating a user, the username, password and phone will not updated.

        It is because of the $input = Input:all()? Please help

  12. [email protected] says

    one more thing: the show() function in UserController dont have a code. How can I use the show() function?

  13. mahesh chinnala says

    ErrorException in Container.php line 734:
    Class html does not exist (View: C:xampphtdocstaskresourcesviewsadmin.blade.php)

  14. Yusuf Badurohman says

    Thanks, it really helped a lot

    1. Deven Rathore says

      you are welcome

  15. genetess aquino says

    I got a problem, in finding my generated controller file. It generates successfully in the cmd but I cannot locate the controller file in the app/http/controllers folder.. :(

    Im using Laravel 5.3 on Windows 7

  16. Muhammad Azizi says

    can i get a full codes??i get a lot of error

  17. Deddy Wahyudi says

    I create Model using the following code: class User extends Eloquent {}

    But there is Something Wrong :
    FatalThrowableError in EloquentUserProvider.php line 120:
    Type error: Argument 1 passed to IlluminateAuthEloquentUserProvider::validateCredentials() must be an instance of IlluminateContractsAuthAuthenticatable, instance of AppUser given, called in C:xampphtdocsdevelopmentvendorlaravelframeworksrcIlluminateAuthSessionGuard.php on line 380

    So Cant you fix it?

  18. Samuel Lawson says

    This illustrates one of the more aggravating things about Laravel — they keep deprecating features that most web frameworks provide out of the box. This Form:: syntax is no longer supported, and even Illuminate is no longer maintained. This has me reconsidering my decision to use Laravel for my project. One now has to add the following to compoer:
    composer require laravelcollective/html

    but who knows what it will be next year. Laravel, I know you want to get minimal, but this was ill-conceived.

  19. morphin paul says

    This is a clear and step by step documentation.we can generate laravel crud with our table name and table fields http://phpcodebuilder.com/crud-generator

Leave A Reply

Your email address will not be published.

Pin It on Pinterest