Diary of problems and solutions related to web programming, database development and related technologies and platforms.

Things to do for setting up Laravel Project

1. Creating a unique Laravel Key for the application or project.

Go to laravelproject > app > config > app.php

Change

key = 'YourSecretKeyGoesHere!'

to

key =''

Save the file and go to command line prompt

C:\wamp\www\laravelproject>php artisan key:generate

2. Configure database settings.

Go to app > config > database.php

Change the details like this. I changed the database name and username as they were not matching according to my database set up.

'mysql' => array(
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'laravelproject',
'username'  => 'root',
'password'  => '',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
),

3. Install and create migration  table

C:\wamp\www\laravelproject>php artisan migrate:install 

Open localhost:phpmydmin and laravelproject databaes. There you will see migrations table created.

4. Creating authors table

C:\wamp\www\laravelproject>php artisan migrate:make create_authors_table

It creates migration file. For this go to app > database > migrations > ......................create_authors_table.php. Open this file.

There you will see CreateAuthorsTable class having up() and down() functions. Up() functions helps to make changes to the database while down() function helps to revert (perfom undo) operations for the changes made at up() function.

Define the up and down functions like this:

public function up()
{
Schema::create('authors', function($table){
$table->increments('id');
$table->string('name');
$table->text('biography');
$table->timestamps();
});
}

public function down()
{
Schema::drop('authors');
}

Execute/run this file via command mode as:

C:\wamp\www\laravelproject>php artisan migrate
Do you really want to run this command? yes

This will create an authors table in the database

Inserting records to the table. Create a class which helps to do that:

C:\wamp\www\laravelproject>php artisan migrate:make add_authors

Open add_authors.php file just created inside migration folder.

Add the following code to add the records:

public function up()
{
DB::table('authors')->insert(array(
'name' => 'Suresh Raj Bhattarai',
'biography' => 'Suresh is a good author',
'created_at' => date('Y-m-d H:m:s'),
'updated_at' => date('Y-m-d H:m:s')
));

DB::table('authors')->insert(array(
'name'=>'Surendra Sedhai',
'biography' => 'Surendra is a good author',
'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', '=', 'Suresh Raj Bhattarai')->delete();
DB::table('authors')->where('name', '=', 'Surendra Sedhai')->delete();
}

Make changes to the table ie add the records by running this again:
C:\wamp\www\laravelproject>php artisan migrate

Look at the table authors. You will see two records added there.

If you want to undo the insertion, the run the rollback command like this:
C:\wamp\www\laravelproject>php artisan migrate:rollback

This rollbacks the last migration done to the database. That is it rollbacks the insertion of the data that took place to the authors table.

To rollback the creation of authors table, run the rollback command again.
C:\wamp\www\laravelproject>php artisan migrate:rollback
This will delete the table authoers.

To migrate everything back, then run the migration command like this
C:\wamp\www\laravelproject>php artisan migrate
This will create authors table and insert the values to the table again.

5. Go to app | views | hello.php

Clear the content inside body tag and type any html content you like. This will change the content of the laravel project for the index page.

Try browsing the page : http://localhost:81/laravelproject/public/

The reason behind displaying this view as hello.php is because of definition on routes file which is located at app > routes.php which has been defined like this way:

Route::get('/', function()
{
return View::make('hello');
});

Another example, if you create about.php inside the same view folder, then you can define routes like this way to open the file:

Route::get('/about', function(){
return View::make('about');
});

Browse the page as :http://localhost:81/laravelproject/public/about

No comments :

Post a Comment