1) Laravel has its own way of creating data tables and performing rollback. This is called Migration.
2) Migration works is done via PHP CLI and artisan script command
3) Install the Migration table to the laraveldb.
3a) Run Command Window. Change directory to {laravel root}.
3b) Ensure that you have registered PHP execution path in Windows Environment. If you are not sure, follow this tutorial.
3c) Run Install command.
3a) Type php artisan migrate:install
3b) Type php artisan migrate:make create_users_table
>php artisan migrate:install Migration table created successfully. >php artisan migrate:make create_users_table Great! New migration created!
4) Edit the table migration settings.
4a) Open {laravel root}\application\migrations\2013_02_02_192721_create_users_table.php
4b) Edit as follows:
<?php class Create_Users_Table { /** * Make changes to the database. * * @return void */ public function up() { Schema::create('users', function($table) { $table->increments('id'); $table->string('email'); $table->string('real_name'); $table->string('password'); $table->timestamps(); }); } /** * Revert the changes to the database. * * @return void */ public function down() { Schema::drop('users'); } }
5) Run migration
(a table migration file is created)
>php artisan migrate Migrated: application/2013_02_02_192721_create_users_table
6) Check the newly created table in laraveldb
(the users table has been created)
(the users table properties is according to the definition in table migration file)
>mysql -h localhost -u laraveladmin -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 527 Server version: 5.5.27 MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use laraveldb; Database changed mysql> show tables; +---------------------+ | Tables_in_laraveldb | +---------------------+ | laravel_migrations | | users | +---------------------+ 2 rows in set (0.00 sec) mysql> select * from laravel_migrations; +-------------+----------------------------------------+-------+ | bundle | name | batch | +-------------+----------------------------------------+-------+ | application | 2013_02_02_192721_create_users_table | 1 | +-------------+----------------------------------------+-------+ 1 rows in set (0.00 sec) mysql> select * from users; Empty set (0.00 sec) mysql> describe users; +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | email | varchar(200) | NO | | NULL | | | real_name | varchar(200) | NO | | NULL | | | password | varchar(200) | NO | | NULL | | | created_at | datetime | NO | | NULL | | | updated_at | datetime | NO | | NULL | | +------------+------------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec) mysql>
7) Perform a migration rollback
>php artisan migrate:rollback Rolled back: application/2013_02_02_192721_create_users_table
8) Check the effect of migration rollback in MySQL Command Window
(the users table has been dropped)
mysql> show tables; +---------------------+ | Tables_in_laraveldb | +---------------------+ | laravel_migrations | +---------------------+ 1 rows in set (0.00 sec) mysql>
9) Run migration again (Step 5)
>php artisan migrate Migrated: application/2013_02_02_192721_create_users_table
this also work but i still can't connect to the mysql server using laravel. when i include DB:query(select * from php);
ReplyDeletein routes that not going to work