This document provides a tutorial on using migrations and Artisan commands in Laravel to manage databases. It demonstrates creating a migration to generate a database table, adding data to the table using another migration, and using Artisan commands like migrate, migrate:rollback and migrate:reset to run and undo the migrations. The migrations handle both creating/modifying the table schema and inserting/deleting sample data from the table. Artisan is the CLI for Laravel that provides commands to interact with migrations and manage the database structure.
4. 4 Author : Vikas Chauhan
Artisan :- Artisan is the name of the command-line interface included with
Laravel. It provides a number of helpful commands.
To view all available Artisan commends type “<project_dir>/ php artisan list “
on your command line.
Migration :- Migrations are a type of version control for your database.
It allows me to create my tables easily, without writing a single line of
database language for example :- mysql etc.
Before install migration in your project you need to be configure your
database file.
8. 8 Author : Vikas Chauhan
On command line :- “php artisan migrate: install” (migration table should be
add in you database)
On command line :- “php artisan migrate: make create_authors_table” (create a
authors table into database)
<project_dir>/ app/ database/ migration
/2013_08_07_101708_create_authors_table.php :-
That php file contain two
function up() & down(). up() function is used for create table and down() function
is used for destroy table and its data. For example :-
9. 9 Author : Vikas Chauhan
class CreateAuthorsTable extends Migration
{
public function up()
{
Schema::create('authors', function($table){
$table->increments('id');
$table->string('name');
$table-> text('bio');
$table->timestamps();
});
}
public function down()
{
Schema::drop('authors');
}
}
10. 10 Author : Vikas Chauhan
On command line :- “php artisan migrate” (for perform action on up function)
On command line :- “php artisan migrate: rollback” (for perform action on down
function)
If you will see fatal error while you rollback the migration :-
on command line :- “var/local/bin/composer.phar dump-autolode” for ubuntu
in your case “<composer_dir>/composer.phar dump-auto lode
On command line :- php artisan migrate:reset (for reset all migration)
11. 11 Author : Vikas Chauhan
Exercise4
Add Data Into Table Using
Migration & Artisan
12. 12 Author : Vikas Chauhan
On command line :- php artisan migrate: make add_authors (create a file in
migration for add data in authors table)
<project_dir>/ app/ database/ migration/
2013_08_07_102350_add_authors.php :-
That php file contain two function up()
& down(). up() function is used for add data into table and down() function is
used for delete data from table. For example :-
13. 13 Author : Vikas Chauhan
class AddAuthors extends Migration
{
public function up() {
DB::table('authors')->insert(array(
'name'=>'vikas chauhan',
'bio'=>'vikas chauhan is a good developer',
'created_at'=>date('y-m-d H:m:s'),
'updated_at'=>date('y-m-d H:m:s')
));
}
public function down() {
DB::table('authors')->where('name','=','vikas chauhan')->delete();
}
}
14. 14 Author : Vikas Chauhan
On command line :- “php artisan migrate” (for perform action on up function)
On command line :- “php artisan migrate: rollback” (for perform action on down
function)
If you will see fatal error while you rollback the migration :-
on command line :- “var/local/bin/composer.phar dump-autolode” for ubuntu
in your case “<composer_dir>/composer.phar dump-auto lode
On command line :- php artisan migrate:reset (for reset all migration)