Laravel Connection Establishment Database

Source: Internet
Author: User

1. Frame setting

first we need to set the Laravel framework, open the application/config/application.php file, we will first set the file key parameter to any 32-bit string:

    1. ' Key ' = ' yoursecretkeygoeshere! ',

This string will be used to encrypt our password. Then we set up the database information in application/config/database.php, where we built it beforehand, and you can name it arbitrarily:

  1. ' MySQL ' =>array (
  2. ' Driver ' = ' mysql ',
  3. ' Host ' = ' localhost ',
  4. ' Database ' = ' database ',
  5. ' Username ' = ' root ',
  6. ' Password ' = ' 123456 ',
  7. ' CharSet ' = ' utf8 ',
  8. ' Prefix ' = ',
  9. ),
  10. 2. Create a database
  11. Then we will use the artisan and migrations tools to build the database, you can simply understand it as a database tool, before using it we need to initialize it. First, add your PHP directory to the environment variable of the system, and then open the CMD tool CD to the root of the Web to run the command:

      1. PHP Artisan Migrate:install

    This is when we entered the database and found a table named Laravel_migrations, which records the data migrate needs. Then we run the following two commands:

      1. PHP Artisan Migrate:make create_admin_table
      2. PHP Artisan Migrate:make create_docs_table

    After a successful run we can see two files named Date _creat_admin_table.php and date _creat_docs_table.php in the Application/migrations directory.

    First open the creat_admin_table.php file and add the code in the Up and down methods:

    1. Publicfunctionup ()
    2. {
    3. Schema::create (' admin ', function ($table)
    4. {
    5. $table->increments (' id ');
    6. $table->string (' email ', 64);
    7. $table->string (' password ', 64);
    8. });
    9. Db::table (' admin ')->insert (Array (
    10. ' Email ' = ' your email ',
    11. ' Password ' =>hash::make (' Your password '),
    12. ));
    13. }
    14. Publicfunctiondown ()
    15. {
    16. Schema::d rop (' admin ');
    17. }

    Re-edit the creat_docs_table.php file:

    1. Publicfunctionup ()
    2. {
    3. Schema::create (' Docs ', function ($table)
    4. {
    5. $table->increments (' id ');
    6. $table->string (' title ', 64);
    7. $table->text (' content ');
    8. $table->string (' Create_date ', 12);
    9. $table->string (' Last_change ', 12);
    10. });
    11. Db::table (' docs ')->insert (Array (
    12. ' Title ' = ' Test ',
    13. ' Content ' = ' Just a test! ',
    14. ' create_date ' =>time (),
    15. ' last_change ' and ' = '
    16. ));
    17. }
    18. Publicfunctiondown ()
    19. {
    20. Schema::d rop (' docs ');
    21. }
    22. After saving, we continue to run the command:PHP artisan Migrate

    23. Database Setup Complete


Laravel Connection Establishment Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.