Laravel Beginners Tutorial 2

Preview:

Citation preview

1 Author : Vikas Chauhan

Presented By:

Vikas Chauhan

Software Engineer,

BrainCoerce

Technologies, Bangalore

Date – 08/18/2013

2 Author : Vikas Chauhan

Exercise 1 :- Migration & Artisan

Exercise 2 :- Database Setup

Exercise 3 :- Create Table Using Migration & Artisan

Exercise 4 :- Add Data Into Table Using Migration & Artisan

3 Author : Vikas Chauhan

Exercise 1

Migration & Artisan

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.

5 Author : Vikas Chauhan

Exercise 2

Database Setup

6 Author : Vikas Chauhan

<project_dir>/app/ config / database.php :-

7 Author : Vikas Chauhan

Exercise 3

Create Table Using Migration & Artisan

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 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 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 Author : Vikas Chauhan

Exercise4

Add Data Into Table Using Migration & Artisan

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 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 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)

15 Author : Vikas Chauhan

Thanks & Regards,Contact to – Vikas Chauhan

Email ID – vikas.chauhan@braincoerce.com

Phone – (080) 41155974