Fast CRUD With Laravel 5

Source: https://gilacoding.com/read/membuat-crud-laravel-hanya-dengan-5-menit

  1. Create a new laravel project named ‘latihan-crud’
    Type this command: composer create-project –prefer-dist laravel/laravel latihan-crud

     
  2. Modify .env file in /laravel/latihan-crud/ directory  to set the database setting:
  3. Create the new database ‘latihan-crud’ on phpmyadmin
  4. Install new package ‘crud-generator’ from ‘appzcoder’: composer require appzcoder/crud-generator
    DONT FORGET TO GO INTO ‘latihan-crud’ DIRECTORY FIRST!

    Look up ‘appzcoder’ directory in /latihan-crud/vendor/
  5. Add the new package ‘Appzcoder\CrudGenerator\CrudGeneratorServiceProvider::class’ in /config/app.php in ‘providers’

     
  6. Install another package ‘laravelcollective/html’

    Then do the same with #5 to add the new package ‘Collective\Html\HtmlServiceProvider::class,’ in /config/app.php in ‘providers’:

     
  7. In the same file (app.php), add the new facades ‘Form’ and ‘HTML’ (????) in ‘aliases’ section:
  8. Do ‘composer update’ to update the dependencies
  9. Then publish our files and setting
  10. Create a new class ‘Posts’ for our crud generator with two fields ‘title’ and
    <OLD>

    BUT I GOT THIS ERROR:

    </OLD
    <NEW>
    From this https://packagist.org/packages/appzcoder/crud-generator, I found the source of the error, I supposed to use ‘;’ (“title#string; body#text”) instead of ‘,’ (“title#string, body#text”) between the fields like this (DONT USE THIS COMMAND. THIS IS ONLY JUST A CORRECT COMMAND)

    USE THIS COMMAND INSTEAD TO MAKE IT CONSISTENT WITH THE ORIGINAL TUTORIAL:

    </NEW>
    The script for the table migration would be exist in /database/migrations/ directory (IF YOU FIND MORE THAN ONE SCRIPT WITH SAME TABLE ‘Posts’ LIKE ‘2016_10_22_081014_create_posts_table.php’ AND ‘2016_10_22_074719_create_posts_table.php’, PLS REMOVE THE OLD ONE FIRST! OR WE’D GET THIS ERROR: “Cannot declare class CreatePostsTable, because the name is already in use”)
  11. Push the table script to our database with ‘php artisan migrate’

    Check our database to make sure the new table ‘posts’ is already created! SOMEHOW THE OTHERS TABLES ‘migrations’, ‘users’ AND ‘password_resets’ ARE CREATED ALSO!
  12. Test on the browser. Start the server: ‘php artisan serve’ then open on our browser : http://localhost:8000/ –> It shows the laravel welcome page!
    Remember, A new route ‘posts’ is already created automatically from previous step, so to see the CRUD in action, pls go to http://localhost:8000/postsposts
  13. Add new data.
    Click the ‘+’ blue icon create-newthen fill the form with the fields ‘Title’ and ‘Body’. Then press ‘Create’ Buttonnew-postBut I got this error:

    I found the solution! In ‘Post’ class in /app/Post.php, at ‘$fillable’ variable, It’s only has ‘title’ field But there is no ‘body’ field!

    So add ‘body’ field like this:

    Then repeat the creation of new data! Here is the expected resultpostsMembuat Login Laravel 5 2 dengan username dan Hak akses
  14. At the right top of the page, there are two links ‘login’ and ‘register’ but none of them are working. To make them work, we need to create the authentication to create users like this: php artisan make:auth

    Dont forget to stop the server first by clicking CTRL+C!
    The above command would create some views for login, register, password, etc. Please check them in /resources/views/auth/ directory!
    This also create automatically a few new routes in /routes/web.php
  15. Now we can try them on our browser
    Remember, we dont need to create/migrate the user table because we already did this at the previous step (#11) when we did the first migration!
    Now the links ‘login’ and ‘register’ are working!
    Here is the register page: http://localhost:8000/registerregisterI use password: satria. Successful registration would be redirected to http://localhost:8000/homelogged-inThe links ‘login’ and ‘register’ also be showed up on the initial page http://localhost:8000/
  16. Change/set default route ‘/’
    In /routes/web.php, the default route for http://localhost:8000/ would go to the welcome page in /resources/views/welcome.blade.php. Here is the default route

    We want to change it to go to our login page! We have the login page in /resources/views/auth/login.blade.php. So here is the modified route

    Now each our homepage http://localhost:8000/ is opened, it’d open the login page!
    If we want to set the default route to the post page, please modify /routes/web.php like this:

    Now, if we open http://localhost:8000/, it’d show the list of the post page!
    LEARN ALSO THIS:
    Laravel Admin Panel
    An admin panel for managing users, roles, permissions & crud.

Leave a Reply

Your email address will not be published. Required fields are marked *